Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active August 21, 2019 08:44
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save westonruter/4b1fca6f20fbc95c3c34 to your computer and use it in GitHub Desktop.
Save westonruter/4b1fca6f20fbc95c3c34 to your computer and use it in GitHub Desktop.
Use DOMDocument to do a more robust job at force_balance_tags.
<?php
/**
* Use DOMDocument to do a more robust job at force_balance_tags.
*
* "force_balance_tags() is not a really safe function. It doesn’t use an HTML parser
* but a bunch of potentially expensive regular expressions. You should use it only if
* you control the length of the excerpt too. Otherwise you could run into memory issues
* or some obscure bugs." <http://wordpress.stackexchange.com/a/89169/8521>
*
* For more reasons why to not use regular expressions on markup, see http://stackoverflow.com/a/1732454/93579
*
* @link http://wordpress.stackexchange.com/questions/89121/why-doesnt-default-wordpress-page-view-use-force-balance-tags
* @see force_balance_tags()
*
* @param string $markup Markup.
* @return string Balanced markup.
*/
function force_balanced_tags2( $markup ) {
$libxml_previous_state = libxml_use_internal_errors( true );
$dom = new DOMDocument();
$html = sprintf(
'<html><head><meta http-equiv="content-type" content="text/html; charset=%s"></head><body>%s</body></html>',
esc_attr( get_bloginfo( 'charset' ) ),
$markup
);
$dom->loadHTML( $html );
$body = $dom->getElementsByTagName( 'body' )->item( 0 );
$markup = str_replace( array( '<body>', '</body>' ), '', $dom->saveHTML( $body ) );
libxml_clear_errors();
libxml_use_internal_errors( $libxml_previous_state );
return $markup;
}
@aristath
Copy link

brilliant, thank you 👍

@westonruter
Copy link
Author

@aristath I just added a couple of (untested) improvements.

@aristath
Copy link

The improvements make sense to me.
Tested them with all kinds of weird markup, no issues so far 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment