首頁 >後端開發 >php教程 >求一段php正则替换规则

求一段php正则替换规则

WBOY
WBOY原創
2016-06-23 14:20:431060瀏覽

/e/tags/index.php?page=23&tagname=%B9%D8%D3%DA%D3%D1%D2%EA%D5%E4%B9%F3%B5%C4%B8%F1%D1%D4&line=3&tempid=13

用preg_replace()把这样的替换成
/tag-%B9%D8%D3%DA%D3%D1%D2%EA%D5%E4%B9%F3%B5%C4%B8%F1%D1%D4/23/

当page=0的时候替换成
/tag-%B9%D8%D3%DA%D3%D1%D2%EA%D5%E4%B9%F3%B5%C4%B8%F1%D1%D4/


回复讨论(解决方案)

$str="/e/tags/index.php?page=0&tagname=%B9%D8%D3%DA%D3%D1%D2%EA%D5%E4%B9%F3%B5%C4%B8%F1%D1%D4&line=3&tempid=13";$s= preg_replace_callback(     '#.+?page=(\d+)&tagname=([^&]+)&.+#',     create_function(            '$matches',            'return "/tag-$matches[2]/". ($matches[1] ? $matches[1]."/" : "");'        ),		$str);echo $s;

/tag-%B9%D8%D3%DA%D3%D1%D2%EA%D5%E4%B9%F3%B5%C4%B8%F1%D1%D4/

$str="/e/tags/index.php?page=0&tagname=%B9%D8%D3%DA%D3%D1%D2%EA%D5%E4%B9%F3%B5%C4%B8%F1%D1%D4&line=3&tempid=13";$s= preg_replace_callback(     '#.+?page=(\d+)&tagname=([^&]+)&.+#',     create_function(            '$matches',            'return "/tag-$matches[2]/". ($matches[1] ? $matches[1]."/" : "");'        ),		$str);echo $s;

/tag-%B9%D8%D3%DA%D3%D1%D2%EA%D5%E4%B9%F3%B5%C4%B8%F1%D1%D4/

非常感谢 每次都是你帮我解决到问题 谢谢
顺便帮我看下这个吧
http://bbs.csdn.net/topics/390575116

$str="/e/tags/index.php?page=0&tagname=%B9%D8%D3%DA%D3%D1%D2%EA%D5%E4%B9%F3%B5%C4%B8%F1%D1%D4&line=3&tempid=13";$s= preg_replace_callback(     '#.+?page=(\d+)&tagname=([^&]+)&.+#',     create_function(            '$matches',            'return "/tag-$matches[2]/". ($matches[1] ? $matches[1]."/" : "");'        ),		$str);echo $s;

/tag-%B9%D8%D3%DA%D3%D1%D2%EA%D5%E4%B9%F3%B5%C4%B8%F1%D1%D4/

请教一下
<a title="总数"> <b>5</b> </a>  <a href="/e/tags/index.php?page=0&tagname=关于友谊珍贵的格言&line=3&tempid=13">首页</a> <a href="/e/tags/index.php?page=0&tagname=关于友谊珍贵的格言&line=3&tempid=13">上一页</a> <a href="/e/tags/index.php?page=0&tagname=关于友谊珍贵的格言&line=3&tempid=13">1</a> <b>2</b>

为什么这一段替换不掉呢?

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:正则下一篇:jpgraph库的问题 求助