highlight_string problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

highlight_string problem

Post by Steveo31 »

Hey all... I'm pulling some code out of a mysql DB and trying to highlight it. Not workin, heh...

I put it in the DB with htmlspecialchars and I can pull it out and echo it just fine. However, I must be using highlight_string the wrong way because it won't show the colors.

Code: Select all

$sql = mysql_query("SELECT title, author, body, hits, rating FROM code LIMIT 0,5");
$row = mysql_fetch_assoc($sql);
$code = nl2br($row['body']);
$code2 = highlight_string($code);
Isn't working. Won't even show parse stuff like > to >.

I've read the manual :)

Thanks.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

since your selecting more than just one recod, should you be doing:

Code: Select all

$sql = "SELECT title, author, body, hits, rating FROM code LIMIT 0,5";
$result =  mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
    //$code = nl2br($row['body']); //you don't want this... but if you want to see a bunch of <br /> tags, than gofor it!
    highlight_string($code);
}
Last edited by Illusionist on Sun May 23, 2004 5:34 pm, edited 2 times in total.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Re: highlight_string problem

Post by jason »

Steveo31 wrote: Isn't working. Won't even show parse stuff like > to >.
It doesn't convert > to >. That means if you have <?php instead of <?php, it won't hightlight correctly.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Illusionist wrote:and then you echo code2??
No.

Reread [php_man]highlight_string[/php_man]. You don't have to echo anything, you just output it unless you explicitly ask it to return a value.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

i just realized that!
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Re: highlight_string problem

Post by Steveo31 »

jason wrote:
Steveo31 wrote: Isn't working. Won't even show parse stuff like > to >.
It doesn't convert > to >. That means if you have <?php instead of <?php, it won't hightlight correctly.
Yeah I know it doesn't convert, but without it the data is parsed correctly. With it, they aren't converted.

Anywho, it works if the data in the DB is not htmlspecialchar'd.

Thanks.
Post Reply