2011-07-21 12 views
0

preg_replaceを使用するには、class="country"のリンクをすべて削除し、リンクのテキストのままにします。php preg_replace

<a href="XXXXXX" class="country">TEXT</a> => TEXT以下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" 
+0

代わりに、DOMパーサを使用してご検討ください。 'england'は削除すべき唯一のリンクテキストです。 – Nik

+0

@Nik、いいえ、私のコードはすべてのリンクを壊しました。私はまだ他のリンクのままにしたい、 'class =" country "' –

答えて

0

これは、ほとんどのケースで動作します:

$result= preg_replace('/<a [^>]*class="country"[^>]*>([^<]+)<\/a>/i','$1',$str); 

しかし、あなたが望むものを得ているように見えます