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

前端 未结 5 2507
囚心锁ツ
囚心锁ツ 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:45

    Although the only fully correct way to do this is not to use regular expressions, you can get what you want if you accept it won't handle all special cases:

    preg_match("/]*?>.*?/i", $str, $match);
    // Use this only if you aren't worried about nested tags.
    // It will handle tags with attributes
    

    And

    preg_replace(""/]*?>.*?/i", "", $str);
    

提交回复
热议问题