DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Text To HTML Converter (PHP 4+)
Simple function to convert a text into formatted HTML in PHP. The function implements some text cleanups (double space removal) and accepts <b>some</b> HTML in the text, like links (a href), lists (ul, ol), blockquotes and tables. This makes it perfect for use inside custom-made blogging engines and CMSs. There's also an implementation of case-insensitive search/replace for php < 5.
<?php
function stri_replace( $find, $replace, $string ) {
// Case-insensitive str_replace()
$parts = explode( strtolower($find), strtolower($string) );
$pos = 0;
foreach( $parts as $key=>$part ){
$parts[ $key ] = substr($string, $pos, strlen($part));
$pos += strlen($part) + strlen($find);
}
return( join( $replace, $parts ) );
}
function txt2html($txt) {
// Transforms txt in html
//Kills double spaces and spaces inside tags.
while( !( strpos($txt,' ') === FALSE ) ) $txt = str_replace(' ',' ',$txt);
$txt = str_replace(' >','>',$txt);
$txt = str_replace('< ','<',$txt);
//Transforms accents in html entities.
$txt = htmlentities($txt);
//We need some HTML entities back!
$txt = str_replace('"','"',$txt);
$txt = str_replace('<','<',$txt);
$txt = str_replace('>','>',$txt);
$txt = str_replace('&','&',$txt);
//Ajdusts links - anything starting with HTTP opens in a new window
$txt = stri_replace("<a href=\"http://","<a target=\"_blank\" href=\"http://",$txt);
$txt = stri_replace("<a href=http://","<a target=\"_blank\" href=http://",$txt);
//Basic formatting
$eol = ( strpos($txt,"\r") === FALSE ) ? "\n" : "\r\n";
$html = '<p>'.str_replace("$eol$eol","</p><p>",$txt).'</p>';
$html = str_replace("$eol","<br />\n",$html);
$html = str_replace("</p>","</p>\n\n",$html);
$html = str_replace("<p></p>","<p> </p>",$html);
//Wipes <br> after block tags (for when the user includes some html in the text).
$wipebr = Array("table","tr","td","blockquote","ul","ol","li");
for($x = 0; $x < count($wipebr); $x++) {
$tag = $wipebr[$x];
$html = stri_replace("<$tag><br />","<$tag>",$html);
$html = stri_replace("</$tag><br />","</$tag>",$html);
}
return $html;
}
?>






Comments
Snippets Manager replied on Mon, 2007/08/27 - 6:38pm
. When the links are translated into html the output this way:
Title of the link one
http://www.mysite.com/clients/client01
/>
Title of the link two
http://mysite.com/clients/client02
/>
This information relates to the link 3:
http://mysite.com/clients/client03
/> ------------------- and this is the php file: <?php // set source file name and path $source = "test2.txt"; // read raw text as array $raw = file($source) or die("Cannot read file"); // retrieve first and second lines (title and author) $slug = array_shift($raw); $byline = array_shift($raw); // join remaining data into string $data = join('', $raw); // replace special characters with HTML entities // replace line breaks with
$html = nl2br(htmlspecialchars($data)); // replace multiple spaces with single spaces $html = preg_replace('/\s\s+/', ' ', $html); // replace URLs with elements $html = preg_replace('/\s(\w+:\/\/)(\S+)/', '\\1\\2', $html); ?>
<?php echo $title; ?>
<?php echo $html; ?> -------------------------- I just don't see where is the mistake, why this: I hope someone can give me a hand. Thank you so much in advance. GerSnippets Manager replied on Fri, 2007/08/03 - 10:56am
$txt = utf8_decode($txt);just before$txt = htmlentities($txt);to avoid output such as "blÃ¥bærsyltetøy" instead of "blåbærsyltetøy".Snippets Manager replied on Sun, 2007/04/15 - 5:25pm
Nick Pao replied on Mon, 2006/09/04 - 8:22am
while( !( strpos($txt,' ') === FALSE ) ) $txt = str_replace(' ',' ',$txt);withif( !( strpos($txt,' ') === FALSE ) ) $txt = str_replace(' ',' ',$txt);It is not a big improvment, but as str_replace is replacing all the matches you don't need a while statement