Skip to content

Instantly share code, notes, and snippets.

@jonathanbardo
Last active December 29, 2018 20:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanbardo/b70c139da0b1a4382f8b to your computer and use it in GitHub Desktop.
Save jonathanbardo/b70c139da0b1a4382f8b to your computer and use it in GitHub Desktop.
Opposite of wpautop
<?php
/**
* Do the opposite of wpautop
*
* @param string $s
*
* @return string
*/
public static function reverse_wpautop( $s ) {
//remove any new lines already in there
$s = str_replace( "\n", '', $s );
//replace <br /> with \n
$s = str_replace( [ '<br />', '<br>', '<br/>'], "\n", $s );
//replace </p> with \n\n
$s = str_replace( '</p>', "\n\n", $s );
// Remove all characters from beginning and end of line
return self::filter_html( $s );
}
/**
* Function to remove all unwanted tags from the html and thus keeping it clean.
*
* @param $input
*
* @param string $add_to_allowed_tag
* @return array|mixed|string
*/
public static function filter_html( $input, $add_to_allowed_tag = '' ) {
// God WordPress this is not very kind
$value = wp_unslash( $input );
// Remove empty paragraph and create
$value = str_replace( [ '&nbsp;', '<p>&nbsp;</p>' ], [ '', '' ], $value );
$value = str_replace( [ '<p></p>' ], [ '' ], $value );
$value = str_replace( [ '<b>', '</b>' ], [ '<strong>', '</strong>' ], $value );
$value = str_replace( [ '<i>', '</i>' ], [ '<em>', '</em>' ], $value );
// Remove unallowed tags
$allowed_tags = '<a><strong><em><ol><ul><li>' . $add_to_allowed_tag;
$value = strip_tags( $value, $allowed_tags );
/**
* Little hack to remove all attributes in an html string
*/
$dom = new \DOMDocument;
libxml_use_internal_errors( true );
/**
* This is to have DOMDocument interpret the string as UTF-8
*/
$dom->loadHTML( '<meta http-equiv="content-type" content="text/html; charset=utf-8">' . $value );
$xpath = new \DOMXPath( $dom );
$nodes = $xpath->query( '//@*' );
foreach ( $nodes as $node ) {
$parent = $node->parentNode;
if ( ! is_object( $parent ) ) {
continue;
}
/**
* We want to keep anchor attributes
*/
if ( 'a' === $parent->tagName && $parent->hasAttributes() ) {
$allowed_attrs = [
'title',
'target',
'href',
];
for ( $i = $parent->attributes->length - 1; $i >= 0; --$i ) {
if ( ! in_array( $parent->attributes->item( 0 )->name, $allowed_attrs ) ) {
$parent->removeAttributeNode( $parent->attributes->item( $i ) );
}
}
} else {
$node->parentNode->removeAttribute( $node->nodeName );
}
}
/**
* Make sure DOMDocument didn't introduce any more unallowed tags like <body>
*/
$value = strip_tags( $dom->saveHTML(), $allowed_tags );
libxml_clear_errors();
return trim( $value );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment