Forum Moderators: coopster

Message Too Old, No Replies

Small echo problem

should be easy to solve

         

skeddy

6:46 pm on Jan 10, 2005 (gmt 0)

10+ Year Member



Back again! (sorry)

I've developed a blog system on my site, and it's working really, really well, bit I noticed today that there is a small bug when browsing the site using Firefox.

Once into the site, the front page displays the last few blogs, with a read more link.

The problem is, I use the following codes to spint out the blogs:

<?php require("config/connections.php");?>
<?php do {?>
<?php require("config/funky.php");?>
<h2><u><?php echo nl2br($output['title']);?></u></h2>
<?php echo nl2br(substr($output['news'],0,300));?>.....<br /></p>
<p><? echo "<a href=\"post.php?id=" . nl2br($output["id"]). "\"> --> Read More?</a>" ;?></p>
<h2><?php echo nl2br($output['date']) ;?></h2>
<hr />
<?php } while ($output = mysql_fetch_array($getnews));?>

The nl2br(substr($output['news'],0,300)) string displays the first 300 charecters from the blog entry, but if during these 300, there is a normal HTML tag that starts but is cut off, the formatting is transferred all the wat though the site. It only happens when browsing wih Firefox. IE seems to understand that I want it to stop.

Is there a way that I could modify my code to finish off the tag so that it's not replicated going down the page?

I would assume that I can't just chuck in all the possible ending tags, becuase this wouldn't make it XHTML compliant.

timster

7:17 pm on Jan 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something like this could work, but you'll also have to check for tags that get cut off inside the tag.

function close_tags($string) {

$iteration = 0;
while (preg_match('/.*(<([^>\/]+)>)(?!.*<\/\2>)/', $string, $matches) ) {

$tag = $matches[2];
$string = $string . "</$tag>";
if (++$iteration > 100) die("Regex problem") ;
}
return $string;
}

willybfriendly

7:27 pm on Jan 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unless there is a compelling reason for HTML in the snippet, why not just use strip_tags($snippet);?

Regarding nl2br, I picked this little function up in these forums a couple of years ago, which works great:

function nl2p($content) {
$content = explode("\n",$content);
foreach($content as $key => $line) {
$lines[$key] = "<p>".$line."</p>";
}
$content = implode("\n",$lines);
return $content;
}

WBF

skeddy

7:48 pm on Jan 10, 2005 (gmt 0)

10+ Year Member



Thanks for the advice.

I modified the code on the following line from:

<?php echo nl2br(substr($output['news'],0,300));?>

To

<?php echo nl2br(strip_tags(substr($output['news'],0,300)));?>

And this works perfectly.

willybfriendly, the function that you describe, what would the purpose of that be? I assume it would have the same effect as using nl2br?

willybfriendly

11:33 pm on Jan 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That function puts <p> tags around paragraphs. Proper markup you know. There are those that believe that content within <p> tags, and its placement, can effect SERPs. Regardless of that, the purist in me thinks that paragraphs should be defined by <p> tags and not by <br><br>.

WBF

Hester

9:46 am on Jan 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<u> is deprecated and should not be used. Use CSS to add underlining instead.