我有这样的网址:
/catalog/kitchen-knives-and-accessories/?clear_cache=Y
By using preg_replace()
I want to make it look like this:
section_code=kitchen-knives-and-accessories
Marked everything bold, which I want from the input in my expected result
Right now I'm using:
#^/catalog/([^/]+)#
作为正则表达式,但是当我将它与 preg_replace()
一起使用时,它没有给我预期的输出。请参阅:http://www.phpliveregex.com/p/dW1
我需要在正则表达式中更改什么才能获得预期结果?
最佳答案
如果你想使用 preg_replace()
你必须匹配整个字符串并简单地用捕获组替换它,例如
$newStr = preg_replace("#^.*/catalog/([^/]+).*#", "section_code=$1", $str);
关于php - preg_replace() 的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258055/