How to replace text over multiple lines using preg_replace

前端 未结 4 526
傲寒
傲寒 2020-12-09 17:46

Hi have the following content within an html page that stretches multiple lines

4条回答
  •  误落风尘
    2020-12-09 18:26

    It is possible to use regex to strip out chunks of html data, but you need to wrap the html with custom html tags which get ignored by browsers. For example:

    This will be shown

    Link

    ... more html in here ...

    This will also be shown
    ';

    To strip the tags with the rel="nofollow" attributes, you can use the following regex:

    $newhtml = preg_replace('/<([^\s]+)[^>]*rel="nofollow"[^>]*>.*?<\/\1>/si', '', $html);
    

    From experience, start the custom tags on a new line. Undoubtedly a hack, but might help someone.

提交回复
热议问题