PO8

autop.php

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