How do I create the correct preg_replace template?

I have these urls in my magento store: charogh / product / 1298 / PRODUCTNAME product name will be changed for each product, correct structure: charogh / product / [0-9] + / [a-zA-z0-9] +

I have to change and replace 'product / [0-9] + /' with '' and make url 'charogh /' when user enters this url 'Charogh / product / [0-9] + /' and do not replace its when user enters this url 'Charogh / product / [0-9] + / PRODUCTNAME' how can I do it using preg_replace or preg_match?

I use this pattern, but it is replaced in both structures $ Title = preg_replace ('// charogh / product / [0-9] + /', "charogh", $ title);

thank you for your help!

+3


source to share


1 answer


I suggest you bind the match to the end of the line using an anchor $

:

'/\/charogh\/product\/[0-9]+\/?$/'

      



This way, you will prevent lines containing more data after 1+ digits from being matched.

In addition, \/?

at the end it allows you to match 1 or 0 /

characters (i.e., /

is optional).

0


source







All Articles