URL、php模式和preg_替换
我正在尝试重写此URL: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
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');