peterdocter 发表于 2013-10-17 12:47

[正则]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>

wjjchen 发表于 2013-10-18 11:15

直接replace x-mw://lookup/不就可以了?

redskywy 发表于 2013-10-19 19:40

preg_replace('/<a href=".*?\/(\w+)(?:\+(\w+))?">/', '<a href="\1 \2">', $str)

peterdocter 发表于 2013-10-22 18:02

存在x-mw://lookup/这样的连接才进行替换...
页: [1]
查看完整版本: [正则]preg_replace替换问题