escaping characters...

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
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

escaping characters...

Post by davidklonski »

Hello

I have a php page with the following line:

Code: Select all

<img src="face.gif" onmouseover="func('<?php print $name; ?>')">
I am using double quotes to enclose the onmouseover value.
I am using single quotes around the name because the func function expects a string.
The problem is that the $name variable may also contain quotes (single or double), and that causes the generated javascript code to misbehave.

I tried using htmlspecialchars & htmlentities but, I am still getting generated code like so:

Code: Select all

<img src="face.gif" onmouseover="func('Guy's name')">
What can I do?

thanks
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Why does this say '0' replies (it will probably say 1 now).
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Aw, the forum is busted :(
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

if you are using php to echo out the line, just add some slashes before the double quotes

Code: Select all

echo "<img src="face.gif" onmouseover="func('Guy's name')">";
If you are not using PHP to echo it out, so its parts of the normal html / js outside of the <? ?> tags, then you don't need to worry about it
Post Reply