Regex / DOMDocument - match and replace text not in a link

后端 未结 7 1098
轮回少年
轮回少年 2020-12-01 07:06

I need to find and replace all text matches in a case insensitive way, unless the text is within an anchor tag - for example:

Match this text and re

相关标签:
7条回答
  • 2020-12-01 07:49
    <?php
    $a = '<p>Match this text and replace it</p>
    <p>Don\'t <a href="/">match this text</a></p>
    <p>We still need to match this text and replace it</p>
    ';
    $res = preg_replace("#[^<a.*>]match this text#",'replacement',$a);
    echo $res;
    ?>
    

    This way works. Hope you want realy case sensitive, so match with small letter.

    0 讨论(0)
提交回复
热议问题