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

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

    You do not want to use regular expressions for this. A much better solution would be to load your contents into a DOMDocument and work on it using the DOM tree and standard DOM methods:

    $document = new DOMDocument();
    $document->loadXML('');
    $document->documentElement->appendChild(
        $document->createFragment($myTextWithTags));
    
    $MY_TAGs = $document->getElementsByTagName('MY_TAG');
    foreach($MY_TAGs as $MY_TAG)
    {
        $xmlContent = $document->saveXML($MY_TAG);
        /* work on $xmlContent here */
    
        /* as a further example: */
        $ems = $MY_TAG->getElementsByTagName('em');
        foreach($ems as $em)
        {
            $emphazisedText = $em->nodeValue;
            /* do your operations here */
        }
    }
    

提交回复
热议问题