Advertisement
Guest User

Untitled

a guest
Aug 30th, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 KB | None | 0 0
  1. function _filter_autop($text) {
  2.  
  3.   // All block level tags
  4.   $block = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|p|h[1-6]|hr)';
  5.  
  6.   // Split at opening and closing PRE, SCRIPT, STYLE, OBJECT, IFRAME tags
  7.   // and comments. We don't apply any processing to the contents of these tags
  8.   // to avoid messing up code. We look for matched pairs and allow basic
  9.   // nesting. For example:
  10.   // "processed <pre> ignored <script> ignored </script> ignored </pre> processed"
  11.   $chunks = preg_split('@(<!--.*?-->|</?(?:pre|script|style|object|iframe|!--)[^>]*>)@i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  12.   // Note: PHP ensures the array consists of alternating delimiters and literals
  13.   // and begins and ends with a literal (inserting NULL as required).
  14.   $ignore = FALSE;
  15.   $ignoretag = '';
  16.   $output = '';
  17.   foreach ($chunks as $i => $chunk) {
  18.     if ($i % 2) {
  19.       $comment = (substr($chunk, 0, 4) == '<!--');
  20.       if ($comment) {
  21.         // Nothing to do, this is a comment.
  22.         $output .= $chunk;
  23.         continue;
  24.       }
  25.       // Opening or closing tag?
  26.       $open = ($chunk[1] != '/');
  27.       list($tag) = preg_split('/[ >]/', substr($chunk, 2 - $open), 2);
  28.       if (!$ignore) {
  29.         if ($open) {
  30.           $ignore = TRUE;
  31.           $ignoretag = $tag;
  32.         }
  33.       }
  34.       // Only allow a matching tag to close it.
  35.       elseif (!$open && $ignoretag == $tag) {
  36.         $ignore = FALSE;
  37.         $ignoretag = '';
  38.       }
  39.     }
  40.     elseif (!$ignore) {
  41.       $chunk = preg_replace('|\n*$|', '', $chunk) . "\n\n"; // just to make things a little easier, pad the end
  42.       $chunk = preg_replace('|<br />\s*<br />|', "\n\n", $chunk);
  43.       $chunk = preg_replace('!(<' . $block . '[^>]*>)!', "\n$1", $chunk); // Space things out a little
  44.       $chunk = preg_replace('!(</' . $block . '>)!', "$1\n\n", $chunk); // Space things out a little
  45.       $chunk = preg_replace("/\n\n+/", "\n\n", $chunk); // take care of duplicates
  46.       $chunk = preg_replace('/^\n|\n\s*\n$/', '', $chunk);
  47.       $chunk = '<p>' . preg_replace('/\n\s*\n\n?(.)/', "</p>\n<p>$1", $chunk) . "</p>\n"; // make paragraphs, including one at the end
  48.       $chunk = preg_replace("|<p>(<li.+?)</p>|", "$1", $chunk); // problem with nested lists
  49.       $chunk = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $chunk);
  50.       $chunk = str_replace('</blockquote></p>', '</p></blockquote>', $chunk);
  51.       $chunk = preg_replace('|<p>\s*</p>\n?|', '', $chunk); // under certain strange conditions it could create a P of entirely whitespace
  52.       $chunk = preg_replace('!<p>\s*(</?' . $block . '[^>]*>)!', "$1", $chunk);
  53.       $chunk = preg_replace('!(</?' . $block . '[^>]*>)\s*</p>!', "$1", $chunk);
  54.       $chunk = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $chunk); // make line breaks
  55.       $chunk = preg_replace('!(</?' . $block . '[^>]*>)\s*<br />!', "$1", $chunk);
  56.       $chunk = preg_replace('!<br />(\s*</?(?:p|li|div|th|pre|td|ul|ol)>)!', '$1', $chunk);
  57.       $chunk = preg_replace('/&([^#])(?![A-Za-z0-9]{1,8};)/', '&amp;$1', $chunk);
  58.     }
  59.     $output .= $chunk;
  60.   }
  61.   return $output;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement