[正则]preg_replace替换问题
<?php$str = <<<EOD
<a href="x-mw://lookup/ new+slangs ">new </a>
<a href="x-mw://lookup/news+ slang ">news </a>
<a href="x-mw://lookup/book">book</a>
EOD;
$pattern='@<a[^>]*href="x-mw://lookup/(?: )([^\+]*)\+([^"]*)"*>([^>]*)</a>@';
$replace="<a href=\"entry://\\1 \\2\">\\3</a>";
$str2=preg_replace($pattern,$replace,$str);
echo $str2."\n"
?>
可以用一条正则完成三种不效果替换吗?
要实现
<a href="new slangs ">new </a>
<a href="news slang ">news </a>
<a href="book">book</a> 直接replace x-mw://lookup/不就可以了? preg_replace('/<a href=".*?\/(\w+)(?:\+(\w+))?">/', '<a href="\1 \2">', $str) 存在x-mw://lookup/这样的连接才进行替换...
页:
[1]