Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/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
URL、php模式和preg_替换_Php_Design Patterns_Preg Replace - Fatal编程技术网

URL、php模式和preg_替换

URL、php模式和preg_替换,php,design-patterns,preg-replace,Php,Design Patterns,Preg Replace,我正在尝试重写此URL: http://www.mysite.com/index.php?topic=54.msg432#msg432 对于这一点: http://www.mysite.com/index.php?thread=54&post=432 使用preg_替换。我试过这个: echo preg_replace('|http?://www\.mysite\.com/index.php/topic=(\d)|', '\1', 'http://w

我正在尝试重写此URL:

http://www.mysite.com/index.php?topic=54.msg432#msg432

对于这一点:

http://www.mysite.com/index.php?thread=54&post=432
使用preg_替换。我试过这个:

echo preg_replace('|http?://www\.mysite\.com/index.php/topic=(\d)|', '\1',
                  'http://www.mysite.com/index.php?topic=54.msg432#msg432');
而且我也没法理解:(我试过使用手册,但是php模式让人困惑。我习惯于python重新模式

这也不行

php -r "echo preg_replace('~\?topic=(\d+).msg(\d+)~', '?thread=$1&post=$2', 'http://www.mysite.com/index.php?topic=54.msg432#msg432');" 

result:
http://www.mysite.com/index.php?thread=&post=#msg432
另外,我会很欣赏一本好的php模式手册

谢谢

试试:

echo preg_replace('~\?topic=(\d+)\.msg(\d+).*~', '?thread=\1&post=\2', 'http://www.mysite.com/index.php?topic=54.msg432#msg432');