Skip to content

Instantly share code, notes, and snippets.

@zachwills
Created August 12, 2015 22:24
Show Gist options
  • Save zachwills/3bd14f489b9e962f7e15 to your computer and use it in GitHub Desktop.
Save zachwills/3bd14f489b9e962f7e15 to your computer and use it in GitHub Desktop.
Clean UTF8 WordPress HTML string for XML use.
/**
* Clean utf8 HTML string for XML.
*
*
* @param string $string HTML string you want to make XML safe.
*/
function clean_html_for_xml( $string ) {
$string = strip_tags( $string );
$string = htmlspecialchars( $string );
$string = strip_shortcodes( $string );
// http://stackoverflow.com/questions/12229572/php-generated-xml-shows-invalid-char-value-27-message
return preg_replace ( '/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment