Created
November 22, 2017 08:14
-
-
Save isu3ru/4be49b4b954a0b554849d2d93fae872a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Replaces double line-breaks with paragraph elements. | |
* | |
* A group of regex replaces used to identify text formatted with newlines and | |
* replace double line-breaks with HTML paragraph tags. The remaining | |
* line-breaks after conversion become <<br />> tags, unless $br is set to '0' | |
* or 'false'. | |
* | |
* @param string $para The text which has to be formatted. | |
* @param bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true. | |
* @return string Text which has been converted into correct paragraph tags. | |
*/ | |
function autop($para, $br = true) { | |
$pre_tags = array(); | |
if ( trim($para) === '' ) | |
return ''; | |
$para = $para . "\n"; // just to make things a little easier, pad the end | |
if ( strpos($para, '<pre') !== false ) { | |
$para_parts = explode( '</pre>', $para ); | |
$last_para = array_pop($para_parts); | |
$para = ''; | |
$i = 0; | |
foreach ( $para_parts as $para_part ) { | |
$start = strpos($para_part, '<pre'); | |
// Malformed html? | |
if ( $start === false ) { | |
$para .= $para_part; | |
continue; | |
} | |
$name = "<pre wp-pre-tag-$i></pre>"; | |
$pre_tags[$name] = substr( $para_part, $start ) . '</pre>'; | |
$para .= substr( $para_part, 0, $start ) . $name; | |
$i++; | |
} | |
$para .= $last_para; | |
} | |
$para = preg_replace('|<br />\s*<br />|', "\n\n", $para); | |
// Space things out a little | |
$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|option|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|noscript|samp|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)'; | |
$para = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $para); | |
$para = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $para); | |
$para = str_replace(array("\r\n", "\r"), "\n", $para); // cross-platform newlines | |
if ( strpos($para, '<object') !== false ) { | |
$para = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $para); // no para inside object/embed | |
$para = preg_replace('|\s*</embed>\s*|', '</embed>', $para); | |
} | |
$para = preg_replace("/\n\n+/", "\n\n", $para); // take care of duplicates | |
// make paragraphs, including one at the end | |
$paras = preg_split('/\n\s*\n/', $para, -1, PREG_SPLIT_NO_EMPTY); | |
$para = ''; | |
foreach ( $paras as $tinkle ) | |
$para .= '<p>' . trim($tinkle, "\n") . "</p>\n"; | |
$para = preg_replace('|<p>\s*</p>|', '', $para); // under certain strange conditions it could create a P of entirely whitespace | |
$para = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $para); | |
$para = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $para); // don't para all over a tag | |
$para = preg_replace("|<p>(<li.+?)</p>|", "$1", $para); // problem with nested lists | |
$para = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $para); | |
$para = str_replace('</blockquote></p>', '</p></blockquote>', $para); | |
$para = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $para); | |
$para = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $para); | |
if ( $br ) { | |
$para = preg_replace_callback('/<(script|style).*?<\/\\1>/s', create_function('$matches', 'return str_replace("\n", "<PreserveNewline />", $matches[0]);'), $para); | |
$para = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $para); // optionally make line breaks | |
$para = str_replace('<PreserveNewline />', "\n", $para); | |
} | |
$para = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $para); | |
$para = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $para); | |
$para = preg_replace( "|\n</p>$|", '</p>', $para ); | |
if ( !empty($pre_tags) ) | |
$para = str_replace(array_keys($pre_tags), array_values($pre_tags), $para); | |
return $para; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment