how to remove a tag and its contents using regular expression?

前端 未结 5 2484
囚心锁ツ
囚心锁ツ 2021-02-20 06:09

$str = \'some text tag contents more text \';

My questions are: How to retrieve content tag contents which is between

5条回答
  •  醉酒成梦
    2021-02-20 06:36

    For removal I ended up just using this:

    $str = preg_replace('~~Usi', "", $str);
    

    Using ~ instead of / for the delimiter solved errors being thrown because of the backslash in the end tag, which seemed to be an issue even with escaping. Eliminating > from the opening tag allows for attributes or other characters and still gets the tag and all of its contents.

    This only works where nesting is not a concern.

    The Usi modifiers mean U = Ungreedy, s = include linebreak characters, i = case insensitive.

提交回复
热议问题