/    Sign up×
Community /Pin to ProfileBookmark

ignoring apostrophes

on my site i have this snippet of code in php

[CODE]

if($width < 1024 && $height < 768){
echo “function loadPicture(pictureName, picAlt)
{ winId = window.open(”,’newwin’,’width=530,height=420′);
winId.document.write(‘<body bgcolor=”black” onLoad=”if (window.focus) window.focus()”><center>’);
winId.document.write(‘<img src=”‘ + pictureName + ‘”>’);
winId.document.write(‘</center><center><font size=”2″ color=”white”>’ + picAlt);
winId.document.write(‘</font></center></body>’);
winId.document.close();
}”;
}
[/CODE]

and then later i call the function

[CODE]
if($width >= 1024 && $height >= 768)
echo “<a href=””.$PHP_SELF.”?width=”.$width.”&height=”.$height.”&type=”.$type.”&offset=”.$offset.”&pic=”.$PICSRC.”” target=”_self”>”;

else
echo “<a href=”JavaScript:loadPicture(‘”.$PICSRC.”‘,'”.$PICALT.”‘)”>”;
[/CODE]

the part where i have a problem is in the else clause in the second snippet of code. it all works fine, unless $PICALT has an apostrophe in the sentence. if it does, it ****s up the rest of the loadPicture(,) function. really what i just need is a way to tell the browser to ignore any apostrophes within $PICALT. i’m sure its really simple and this was an insanely long winded way of asking it, but i’d appreciate the help. sorry for the php, but it shouldn’t affect the answer to my question any. thanks again

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@PittimannJun 19.2004 — Hi!

If you replace the unescaped double quotes with single ones and vice versa in your PHP, you can leave single quotes in your $PICALT untouched:[code=php]echo '<a href="javascript:loadPicture("'.$PICSRC.'","'.$PICALT.'")">';[/code]Please note: incase you copy and paste the line of code, make sure to remove the space between java and script.

PHP doesn't have a problem when single quotes are used with echo and js doesn't have a problem if arguments passed to a function are double quoted.

Cheers - Pit
Copy linkTweet thisAlerts:
@thriceauthorJun 19.2004 — thanks for the reply, but its still not working.the the double quotes are gonna interfere with href's double quotes....right? so far i've had no luck with that way either....
Copy linkTweet thisAlerts:
@PittimannJun 19.2004 — Hi!

That's what I get for not testing (getting rusty in PHP) - sorry!

I put a working example online here: http://www.pit-r.de/scripts/escapeSlashes.php

The code is:[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<?
echo "<script language="JavaScript" type="text/javascript">rn";
echo "<!--rn";
echo "function loadPicture(pictureName, picAlt){rn";
echo "winId = window.open('','newwin','width=530,height=520');rn";
echo "winId.document.write('<body bgcolor="black" onLoad="if (window.focus) window.focus()"><center>');rn";
echo "winId.document.write('<img src="' + pictureName + '">');rn";
echo "winId.document.write('</center><center><font size="2" color="white">' + picAlt);rn";
echo "winId.document.write('</font></center></body>');rn";
echo "winId.document.close();rn";
echo "}";
echo "//-->rn";
echo "</script>rn";
?>
</head>
<body>
<?
$PICALT="Mike's bike";
$PICSRC="imgMaps/bike2.jpg";
echo '<a href="#" onclick="loadPicture(&quot;'.$PICSRC.'&quot;,&quot;'.$PICALT.'&quot;)">click</a>';
?>
</body>
</html>[/code]
Cheers - Pit
Copy linkTweet thisAlerts:
@thriceauthorJun 19.2004 — ahh...clever man =) wonder how long that woulda taken me on my own. anyways, thanks again for the help. i'll try that out when i get home... busy 'fixing' computers at best buy now =)
×

Success!

Help @thrice spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...