preg_replace字符串(?)

时间:2014-04-21 03:03:15

标签: php preg-replace

我有一组字符串,想要替换

/church-ceremony-venues/all/in-quezon-city/church?page=2

/church-ceremony-venues/all/in-quezon-city?page=2

我尝试按照question的回答,但没有任何反应。

2 个答案:

答案 0 :(得分:0)

$str = "/church-ceremony-venues/all/in-quezon-city/church?page=2";
echo preg_replace("/\/church\?/", "?", $str);

答案 1 :(得分:0)

在最后/之前和?之后获取所有字符,然后您的新字符串包含已捕获的论坛$1$2

$input_lines = "/church-ceremony-venues/all/in-quezon-city/church?page=2";

echo preg_replace("/.([^\?]+)\/.+(\?.+)/", "/$1$2", $input_lines);

查看php live regex

的示例