$str =
str_replace("\n\n", "</p>\n\n<p>",
$str);
// Convert single spaces to <br> tags
$br = '<br' .
_solidus() . '>';
$str =
preg_replace("/([^\n])(\n)([^\n])/", '\\1' .
$br . '\\2\\3',
$str);
// Wrap the whole enchilada in enclosing paragraphs
if ($str !== "\n"
) { // We trim off the right-side new line so that the closing </p> tag
// will be positioned immediately following the string, matching
// the behavior of the opening <p> tag
$str = '<p>' .
rtrim($str) . '</p>';
} // Remove empty paragraphs if they are on the first line, as this
// is a potential unintended consequence of the previous code
return preg_replace('/<p><\/p>(.*)/', '\\1',
$str, 1
);
} /**
* Protect Characters
*
* Protects special characters from being formatted later
* We don't want quotes converted within tags so we'll temporarily convert them to {@DQ} and {@SQ}
* and we don't want double dashes converted to emdash entities, so they are marked with {@DD}
* likewise double spaces are converted to {@NBS} to prevent entity conversion
*/