Welcome to WebmasterWorld Guest from 54.145.209.34

Forum Moderators: coopster & jatar k

question regarding html tags in echo

tags, echo, php

   
5:06 am on May 9, 2011 (gmt 0)



So I've been playing with this random quote generator that is supposed to change the font style based on length. All of that stuff is working more or less as intended (as in, I've not noticed anything screwy with it), but if it displays a quotation that was stored with tags (specifically, the break tag <br>), the browser doesn't execute them.

I was wondering why. Is it a case of improper nesting of php/html?

The entire script follows (barring the database access portion).

<?php
[connect to database stuff]
if( $length <= 250)
$font = h1;
elseif( $length <= 400)
$font = 'h2';
elseif( $length <=1300)
$font = 'h3';
elseif( $length <= 2200)
$font = 'h5';
elseif( $length >2200)
$font='h7';
$source = htmlentities(mysql_result($result,$i,"source"));
?>
<<?php echo $font; ?>>
<?
echo($quote . "<p><i>" . $source . "</p></i>");
mysql_close($connection);
?>
</<?php echo $font ?>>
6:51 am on May 9, 2011 (gmt 0)

5+ Year Member



Well you're calling htmlentities on source. So any < would be converted to &lt;

I can't really see where you're building $quote, but you might be performing a similar action on that
7:19 am on May 9, 2011 (gmt 0)



My bad, I forgot to leave in the other sql instructions:
@mysql_select_db($database) or die( "Unable to select database"); 
$query="SELECT `quote`, `source` FROM `quote` ORDER by rand() LIMIT 1";
$result=mysql_query($query) or die(mysql_error());
$quote = htmlentities(mysql_result($result,$i,"quote"));


Whenever it populates in the page, it actually shows as
blah blah blah<br>blah blah blah<br>
...if that helps
4:49 pm on May 9, 2011 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The first answer is still correct. :-) htmlentities() [php.net]
6:53 pm on May 9, 2011 (gmt 0)



I didn't doubt the answer, I just wasn't really clear on what htmlentities() was doing (and I did look it up after Tommybs' reply -- I'm definitely a greenhorn newb in php/html):

when <br> is washed through htmlentities(), the < changes to &lt (and I assume something similar for the >). So when that &lt gets to the browser, the browser then replaces it with "<"?
4:20 pm on May 10, 2011 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Correct, which is often a good thing - for example, ampersands in query strings won't validate:

script.php?this=that&these=those&his=hers

htmlentities on those strings converts them correctly (and the links will still work)

script.php?this=that&amp;these=those&amp;his=hers
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month