homepage Welcome to WebmasterWorld Guest from 54.196.196.62
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
question regarding html tags in echo
tags, echo, php
siloxr




msg:4309716
 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 ?>>

 

Tommybs




msg:4309721
 6:51 am on May 9, 2011 (gmt 0)

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

siloxr




msg:4309725
 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
rocknbil




msg:4309927
 4:49 pm on May 9, 2011 (gmt 0)

The first answer is still correct. :-) htmlentities() [php.net]

siloxr




msg:4310016
 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 "<"?

rocknbil




msg:4310387
 4:20 pm on May 10, 2011 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved