php - Replace last occurrence to end of string using Twig preg_replace
Get the solution ↓↓↓Solution:
You can use this:
preg_replace("/-\d\d(-.*|)$/","","Coverking_CSC_Neosupreme_1Row-F-150_Series-01-MASS");
Answer
Solution:
If you want to strip anything from the-XX-
up until the end, use the$
anchor character to define you want to match up until the last character of the string.
You can do something like this:
{{ str|preg_replace("/\-[0-9]{2}(\-[\w\-]+)?$/", "") }}
The \w matches 0-9 A-Z and a-z.
See regex in action:
https://regex101.com/r/aiKMDn/2
edit:
Updated regex to have anything after-XX
as optional match up until the end of the string.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: an exception occurred in the driver: could not find driver
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.