如何使用preg_replace,使用class="country"删除所有链接,并保留链接的文本。
<a href="XXXXXX" class="country">TEXT</a> =>文本
如何修改下面的preg_replace?
$str = <<<EOT
Long long ago, there was a poor boy named <a href="index1.html">John</a>. Who was living in <a href="index2.html">Liverpoor</a>, a city of <a href="index3.html" class="country">england</a>.
EOT;
$result= preg_replace('/<a(.*?)class="country"(.*?)>(.*?)<\/a>/i','$3',$str);
echo $result;
// I want get the result as: "Long long ago, there was a poor boy named <a href="index1.html">John</a>. Who was living in <a href="index2.html">Liverpoor</a>, a city of england"发布于 2011-07-21 23:28:24
这将适用于大多数情况:
$result= preg_replace('/<a [^>]*class="country"[^>]*>([^<]+)<\/a>/i','$1',$str);但请考虑使用DOM解析器
https://stackoverflow.com/questions/6778120
复制相似问题