gpt4 book ai didi

php - preg_replace() 的意外结果

转载 作者:搜寻专家 更新时间:2023-10-31 21:27:19 26 4
gpt4 key购买 nike

我有这样的网址:

/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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com