Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
preg/str替换为通配符php_Php_String_Escaping_Wildcard - Fatal编程技术网

preg/str替换为通配符php

preg/str替换为通配符php,php,string,escaping,wildcard,Php,String,Escaping,Wildcard,要使用/page-1重新拼合/randomword/(通配符)/ 所以url将被替换为 当我不知道什么时候该逃避时,不工作使我感到困惑 (阅读)str_replace()没有通配符;您希望改用preg_replace() 试试这个: $url= preg_replace('~/(.*?)/fixed~', '/randomword/page-1/fixed', $url); 此外,这里还有一个很好的工具,可以帮助您用不同的语言构建正则表达式: 进一步阅读: 非常感谢@chris85

要使用/page-1重新拼合/randomword/(通配符)/ 所以url将被替换为

当我不知道什么时候该逃避时,不工作使我感到困惑


(阅读)

str_replace()没有通配符;您希望改用preg_replace()

试试这个:

$url= preg_replace('~/(.*?)/fixed~', '/randomword/page-1/fixed', $url);
此外,这里还有一个很好的工具,可以帮助您用不同的语言构建正则表达式:

进一步阅读:


非常感谢@chris85,他提供了这个答案的大部分信息

正则表达式需要PHP中的分隔符。我读过了,但似乎太复杂了,没有例子。这里有一些例子。第一个
/
是开始分隔符,最后一个
/
是结束分隔符。中间的一切都是正则表达式。从第一个链接注意:
如果需要在模式内部匹配分隔符,则必须使用反斜杠对其进行转义。如果分隔符经常出现在模式中,则最好选择另一个分隔符以增加可读性。
。。。所以,
~/(.*?)/fixed~
我想这会解决你的问题。您可能更喜欢任何非
/
而不是
(.*?
)。如果
/fixed/
是常量,您可以通过在其上预先添加新目录来替换它。可能的
$url= preg_replace('~/(.*?)/fixed~', '/randomword/page-1/fixed', $url);