<?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; } ?>
$txt = utf8_decode($txt);
just before
$txt = htmlentities($txt);
to avoid output such as "blÃ¥bærsyltetøy" instead of "blåbærsyltetøy".
This code converts a text file into html via php. Everything is fine it it is just the urls from the text file that have an extra <br/>. When the links are translated into html the output this way:
<h3></h3>
Title of the link one
http://www.mysite.com/clients/client01<br</a> />
Title of the link two
http://mysite.com/clients/client02<br</a> />
This information relates to the link 3:
http://mysite.com/clients/client03<br</a> />
-------------------
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);
?>
<h3><?php echo $title; ?></h3>
<?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.
Ger
You need to create an account or log in to post comments to this site.
with
It is not a big improvment, but as str_replace is replacing all the matches you don't need a while statement