&copy;) and converts < and > into * entities. If wanted, all p's and br's can be removed as well, * just uncomment line 18. */ function convert2char($string) { encodeEntities($string); escapeXML($string); // xml2nl($string); return $string; } /** * Used to strip all \ when a preview is performed. */ function stripPOST() { foreach ($_POST as $key => $value) { if (is_string($value)) $_POST[$key] = stripslashes($value); } } /** * This function can be used to remove all p's and br's from * a text. Problem: it removes user defined p's as well. */ function xml2nl(&$string) { $string = str_replace(array('

', '

', '
'), '', $string); } /** * Influensed by autop at photomatt.net. Paragraph creation is basically performed * the same way. The differens is in the handling of nested p's and code|pre-blocks. */ function nl2xml(&$string) { $pattern = array( "|
(\s*)
|i", "/(\r\n|\r)/", "/\n\n+/", //Paragraphs "/\n?(.+?)(?:\n\s*\n|\z)/s", "|

\s*?

|", //Remove blank segments //Handle blockquotes "/

]*)>/i", "|

|i", //Remove paragraphs around block elements "!

\s*(]*>)!", "!(]*>)\s*

!", //Line breaks "/(?)\n(?!(\n)|(\<))/i", "/
$/i", //Remove paragraphs and br's inside code and pre. "/(<(code|pre)([^>]*)>)(.*?)(<\/(code|pre)>)/ise", //Fix paragraphs inside list elements. "/(<(li)([^>]*)>)(.*?)(<\/(li)>)/ise" ); $replacement = array( "\n\n", "\n", "\n\n", "

\\1

\n\n", '', "

", "

", "\\1", "\\1", "
\n", '', "'\\1'. p2br('\\4') .'\\5'", "'\\1'. pfix('\\4') .'\\5'" ); if (!empty($string)) { $string = preg_replace($pattern, $replacement, $string); } } /** * Replace p's with br when nested inside code or pre */ function p2br($str) { $str = str_replace('\\"', '"', $str); $str = preg_replace('/<\/p>(\s*\n\s*)

/i', "\n \n", $str); return str_replace('
', '', $str); } /** * Fix p's that are nested inside li's. */ function pfix($str) { $str = str_replace('\\"', '"', $str); //Manually remove slashes. if (strstr($str, '

') && strpos($str, '

') > 0) $str = '

'. $str . '

'; return $str; } /** * Escape stand-alone & by addning & to them */ function decodeEntities(&$string) { $string = preg_replace('/&(\s|\n)/', '&\\1', $string); } /** * Convert all & into &. Used to print entities AS entities and NOT AS * the symbol they represent. E.g. © should be displayed, NOT ©. */ function encodeEntities(&$string) { $string = str_replace('&', '&', $string); return $string; } /** * This method does a simple replacement of < and >. Used to make the text * editable in a textarea. */ function escapeXML(&$string) { $string = str_replace(array('<', '>'), array('<', '>'), $string); } ?>