替换<a> with attributes

时间:2015-08-11 06:43:40

标签: hyperlink preg-replace

I'm trying to preg_replace a link caption as below. Can't find an example tho where replacing would consider tag attributes, not just clean tags

Basically, this

  <a href="/some/address" class="active">Database Title</a> 

needs to become this

  <a href="/some/address" class="active">My Own Title</a> 

Help appreciated

1 个答案:

答案 0 :(得分:1)

您可以使用正则表达式和后向引用

$html = '<a href="/some/address" class="active">Database Title</a>;
$html = preg_replace("/(<.+>).+(<.+>)/", "$1My Own Title$2", $html);

echo $html;

http://www.php.net/manual/en/regexp.reference.back-references.php