如何创建正确的preg_replace模式?

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

I should change and replace 'product/[0-9]+/' with '' and make the url to 'charogh/' when user enter this url 'charogh/product/[0-9]+/' and do not replace it when user enter this url 'charogh/product/[0-9]+/productname' how can i do it with preg_replace or preg_match?

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

thanks for your help!

I suggest you anchor the match at the string end with the $ anchor:

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

This way you will prevent matching strings that contain more data after 1+ digits.

Also, the \/? at the end allows matching 1 or 0 / symbols (i.e. the / is optional).