scrybs scrybs - 1 year ago 172
PHP Question

PHP preg_replace html tag containing line breaks

I'm trying to remove a certain html tag with preg_replace but I can't find any way to do it, it's working if I remove the line breaks but not with.

The regex so far:

preg_replace("/<ol class=\"comment-list\">.*?<\/ol>/", "", $string);

The string in question:

<ol class="comment-list">
<time datetime="2016-03-25T15:27:34+00:00"></ol>

I'm using to test it.

Thanks a lot for your help!

Answer Source

I know that probably this answer is not what you want, but if you want try, this is how you can remove <ol> nodes using DOMDocument:

$dom = new DOMDocument();           // Init DOMDocument object
libxml_use_internal_errors( True ); // Disable libxml errors
$dom->loadHTML( $html );            // Load HTML
$xpath = new DOMXPath( $dom );      // Init DOMXPath (useful for complex queries)

/* Search for all <ol> nodes with class “comment-list”: */
$nodes = $xpath->query( '//ol[@class="comment-list"]' );
/* Remove nodes: */
while( $nodes->length )
    $nodes->item(0)->parentNode->removeChild( $nodes->item(0) );

/* Output modified HTML: */
echo $dom->saveHTML();

Yes, these are 7 lines versus one, but I suggest you this way. Regular expressions are a great invention, but not for HTML/XML.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download