PHP Classes

File: fwphp/glomodul/z_examples/book_video/03steinmetz_2008/ch05_txt_html/autop.php

Recommend this page to a friend!
  Classes of Slavko Srakocic  >  B12 PHP FW  >  fwphp/glomodul/z_examples/book_video/03steinmetz_2008/ch05_txt_html/autop.php  >  Download  
File: fwphp/glomodul/z_examples/book_video/03steinmetz_2008/ch05_txt_html/autop.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: B12 PHP FW
Manage database records with a PDO CRUD interface
Author: By
Last change:
Date: 3 months ago
Size: 1,878 bytes
 

Contents

Class file image Download
<?php

function autop($pee, $br = 1) {
//Converts plain text to HTML-compliant stuff

   
$pee = $pee . "\n"; // just to make things a little easier, pad the end
   
$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
   
$pee = preg_replace('!(<(?:table|ul|ol|li|pre|form|blockquote|h[1-6])[^>]*>)!', "\n$1", $pee); // Space things out a little
   
$pee = preg_replace('!(</(?:table|ul|ol|li|pre|form|blockquote|h[1-6])>)!', "$1\n", $pee); // Space things out a little
   
$pee = preg_replace("/(\r\n|\r)/", "\n", $pee); // cross-platform newlines
   
$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
   
$pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "\t<p>$1</p>\n", $pee); // make paragraphs, including one at the end
   
$pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
   
$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
   
$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
   
$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
   
$pee = preg_replace('!<p>\s*(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)!', "$1", $pee);
   
$pee = preg_replace('!(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee);
    if (
$br) $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
   
$pee = preg_replace('!(</?(?:table|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*<br />!', "$1", $pee);
   
$pee = preg_replace('!<br />(\s*</?(?:p|li|div|th|pre|td|ul|ol)>)!', '$1', $pee);
   
$pee = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $pee);
   
    return
$pee;
}

print
autop(' hi there<p>
<table><tr><td>foo
</td>
</tr></td>
blah'
);
?>

For more information send a message to info at phpclasses dot org.