...

View Full Version : echo html code - how to escape underscore



mypointofview
12-01-2006, 02:41 AM
I want to echo some html code that contains underscores (in certain file names). I use the backslash to escape all quotes. I tried this also with the underscores that posed a problem but the backslash gets echoed.

The problem occurs only twice in my code, when the underscore happens to be right after $random

Is there some trick I could use? I have searched like crazy and the "stripslashes" technique comes to my mind but I'm not using a form or so, just want to echo simple string of html.

Can somebody help?

Thanks, Martin.



<?php

$total = "9";

$start = "1";

$random = mt_rand($start, $total);


echo "<a href=\"javascript:pop_0$random_horizontal()\" title=\"Click to enlarge\" onMouseover = \"document.thumb0$random.src=thumb_0$random\_over.src\" onMouseout = \"document.thumb0$random.src=thumb_0$random.src\"><img src=\"thumbs/thumb_0$random/thumb_darker.jpg?id=$randy\" alt=\"TheAltText\" name=\"thumb0$random\"></a>";

?>

PS: Of course I could just adapt my code replacing the underscores but I find them optically useful when coding.

chump2877
12-01-2006, 03:03 AM
You don;t need to escape underscores in PHP or javascript.

Tyree
12-01-2006, 03:49 AM
Heck, you can echo that line without escaping anything...just do it like this:


echo '<a href="javascript:pop_0$random_horizontal()" title="Click to enlarge" onMouseover = "document.thumb0$random.src=thumb_0$random_over.src" onMouseout = "document.thumb0$random.src=thumb_0$random.src"><img src="thumbs/thumb_0$random/thumb_darker.jpg?id=$randy" alt="TheAltText" name="thumb0$random"></a>';


Use ' instead of " around the string declaration.

mypointofview
12-01-2006, 07:12 AM
<a href="javascript:pop_0()" title="Click to enlarge" onMouseover = "document.thumb04.src=thumb_0.src" onMouseout = "document.thumb04.src=thumb_04.src"><img src="thumbs/thumb_04/thumb_darker.jpg?id=fd4cdc28b3aeb64fb961f2d25ffa7293" alt="TheAltText" name="thumb04"></a>

R.J., looking at the page html source within Firefox, above you see what I get when I don't excape ANY underscore :eek: Note that the underscore after a number will blank out all until the parethesis () (javascript part at beginning) or the .src (mouse over). The other underscores are OK.

I read that CSS 2.1 does not like underscores after numbers - but we're here in PHP. I tested it only on my computer running PHP 5.1.4.

Tyree, below is what i see in Firefox when using single quotes instead of double quotes - the backslashes are all there as well as the php part :eek:


<a href=\"javascript:pop_0$random_horizontal()\" title=\"Click to enlarge\" onMouseover = \"document.thumb0$random.src=thumb_0$random_over.src\" onMouseout = \"document.thumb0$random.src=thumb_0$random.src\"><img src=\"thumbs/thumb_0$random/thumb_darker.jpg?id=$randy\" alt=\"TheAltText\" name=\"thumb0$random\"></a>

</body>

Below the original PHP ("random9.txt") code (in this case with single quotes) which I include in the final html :


<?php

/* Add a number to the items or to the folders without leading zero if more than 9 variables. Add the following line where the variable part of the file path would normally be: <?php include "/phpincludes/randompix_xxxx.txt"; ?> That line must include the name and file path to this document here. Replace xxxx with a descriptive word. */


// Change the following number to the total number of random variables.
$total = "9";

// No need to edit below this line.

$start = "1";

$random = mt_rand($start, $total);


echo '<a href=\"javascript:pop_0$random_horizontal()\" title=\"Click to enlarge\" onMouseover = \"document.thumb0$random.src=thumb_0$random_over.src\" onMouseout = \"document.thumb0$random.src=thumb_0$random.src\"><img src=\"thumbs/thumb_0$random/thumb_darker.jpg?id=$randy\" alt=\"TheAltText\" name=\"thumb0$random\"></a>';

?>

Below is the html code of the original page as seen in my text editor. It's a test with one picture. The goal is to have random (rollover) images which have a link to another bigger image.


What I'm basically doing :

- "randy" code to overcome Explorer's cache problem
- rollovers
- popup (via JavaScript - not yet loaded on the test page)


<?php include "http://www.mywebsite.com/phpincludes/header.txt"; ?>

<!-- Rollover thumbnails are on the page. Only needed if pictures shall change when rolling the cursor over them. -->
<!-- The Java Script below must reside INSIDE this page, because the php random "randy" number would otherwise not be generated. -->

<script type="text/javascript" language="JavaScript">
<!--

// This list must contain in minimum the amount of the most thumb rollovers found on this page.
// The random ("randy") number code is only necessary to force InternetExplorer to update the rollover image and not use the image from its cache.

if (document.images) {

thumb_01 = new Image; thumb_01_over = new Image;
thumb_01.src = "thumbs/thumb_01/thumb_darker.jpg?id=<?php echo($randy); ?>"; thumb_01_over.src = "thumbs/thumb_01/thumb.jpg?id=<?php echo($randy); ?>"

thumb_02 = new Image; thumb_02_over = new Image;
thumb_02.src = "thumbs/thumb_02/thumb_darker.jpg?id=<?php echo($randy); ?>"; thumb_02_over.src = "thumbs/thumb_02/thumb.jpg?id=<?php echo($randy); ?>"

thumb_03 = new Image; thumb_03_over = new Image;
thumb_03.src = "thumbs/thumb_03/thumb_darker.jpg?id=<?php echo($randy); ?>"; thumb_03_over.src = "thumbs/thumb_03/thumb.jpg?id=<?php echo($randy); ?>"

thumb_04 = new Image; thumb_04_over = new Image;
thumb_04.src = "thumbs/thumb_04/thumb_darker.jpg?id=<?php echo($randy); ?>"; thumb_04_over.src = "thumbs/thumb_04/thumb.jpg?id=<?php echo($randy); ?>"

thumb_05 = new Image; thumb_05_over = new Image;
thumb_05.src = "thumbs/thumb_05/thumb_darker.jpg?id=<?php echo($randy); ?>"; thumb_05_over.src = "thumbs/thumb_05/thumb.jpg?id=<?php echo($randy); ?>"

thumb_06 = new Image; thumb_06_over = new Image;
thumb_06.src = "thumbs/thumb_06/thumb_darker.jpg?id=<?php echo($randy); ?>"; thumb_06_over.src = "thumbs/thumb_06/thumb.jpg?id=<?php echo($randy); ?>"

thumb_07 = new Image; thumb_07_over = new Image;
thumb_07.src = "thumbs/thumb_07/thumb_darker.jpg?id=<?php echo($randy); ?>"; thumb_07_over.src = "thumbs/thumb_07/thumb.jpg?id=<?php echo($randy); ?>"

thumb_08 = new Image; thumb_08_over = new Image;
thumb_08.src = "thumbs/thumb_08/thumb_darker.jpg?id=<?php echo($randy); ?>"; thumb_08_over.src = "thumbs/thumb_08/thumb.jpg?id=<?php echo($randy); ?>"

}

// -->
</script>




</head>
<body>

<?php include "phpincludes/random9.txt"; ?>

</body>
</html>

CFMaBiSmAd
12-01-2006, 07:48 AM
Pretty simple, put the variable in {...} when it is in side of a double-quoted string - {$random}

echo "<a href=\"javascript:pop_0{$random}_horizontal()\" title=\"Click to enlarge\" onMouseover = \"document.thumb0$random.src=thumb_0$random\_over.src\" onMouseout = \"document.thumb0$random.src=thumb_0$random.src\"><img src=\"thumbs/thumb_0$random/thumb_darker.jpg?id=$randy\" alt=\"TheAltText\" name=\"thumb0$random\"></a>";
Because an under-score is permitted in variable names, the PHP parser does not know and cannot determine where the variable name ends. This is the same problem PHP has with array/[] within double-quoted strings.

Making use of error_reporting(E_ALL) will expose how PHP is treating the code in the original post.

Edit: For the case of the single-quote suggestion, variables are not parsed within single-quoted strings. The double-quotes and the variable name contained within the single-quoted string are literally just the characters - " and $ r a n d o m and they have no special meaning.

Tyree
12-01-2006, 01:18 PM
What do you mean "variables are not parsed within single-quoted strings". I have 100+ pages where I've used single quotes in such cases. Works fine for me.
Case in point: http://tyreeonline.com/tests/single_quotes/

The code:


<?php
$random = "--RANDOMVAR--";
$randy = "--RANDYVAR--";
$variable = '<a href="http://www.google.com">A Text Link</a>';
$varImage = '<a href="http://www.google.com"><img src="logo_with_underscores.gif" border="0" /></a>';
$yourLink = '<a href="javascript:pop_0'.$random.'_horizontal()" title="Click to enlarge" onMouseover = "document.thumb0'.$random.'.src=thumb_0'.$random.'_over.src" onMouseout = "document.thumb0'.$random.'.src=thumb_0'.$random.'.src"><img src="thumbs/thumb_0'.$random.'/thumb_darker.jpg?id='.$randy.'" alt="TheAltText" name="thumb0'.$random.'"></a>';

print "An image link:<br /><br />".$varImage."<br /><br />".$variable."<br /><br />Your Link:<br />".$yourLink;
?>


Now, maybe I'm not totally understanding what the problem is. But, you can definitely single quote variable string declarations.

For your $random and $randy vars throughout your link, just concatenate them into the string.
$string = 'string text' . $var . 'more string text';

You can view the source in Firefox and IE. It's printing your link with the correct variable values perfectly.

Oh, you may want to add border="0" to your image link too. ;)

GJay
12-01-2006, 02:39 PM
variables are not parsed within single quoted strings


$test='hello';
echo '$test';
//compared with
echo "$test";

will output:


$testhello

Tyree
12-01-2006, 02:50 PM
Ah, I see what you mean. But, still, as long as you concatenate them into the string like I did in the example, it's all good.

mypointofview
12-01-2006, 08:49 PM
CFMaBiSmAd, your trick with replacing $random with {$random} works great. Thank you. :thumbsup:

Tyree, single quotes don't work for me when put in front of and at the end of the output html (the echo line). Maybe it works in your case because you run a different version of PHP? But wait, I see that in your code you indeed do use double quotes for the print part of the code. ;)

I still don't know the difference between echo and print, but that's perhaps another thread...

Thanks guys, I learned a lot here.

CFMaBiSmAd
12-01-2006, 09:00 PM
http://www.php.net/manual/en/language.types.string.php

Tyree
12-01-2006, 09:09 PM
Tyree, single quotes don't work for me when put in front of and at the end of the output html (the echo line). Maybe it works in your case because you run a different version of PHP? But wait, I see that in your code you indeed do use double quotes for the print part of the code. ;)

Not that it really matters since the {} worked for you...
It works just as well like this:


echo '<a href="javascript:pop_0'.$random.'_horizontal()" title="Click to enlarge" onMouseover = "document.thumb0'.$random.'.src=thumb_0'.$random.'_over.src" onMouseout = "document.thumb0'.$random.'.src=thumb_0'.$random.'.src"><img src="thumbs/thumb_0'.$random.'/thumb_darker.jpg?id='.$randy.'" alt="TheAltText" name="thumb0'.$random.'"></a>';


See? Bottom line: http://tyreeonline.com/tests/single_quotes/

I use double-quotes as a default. But, if I have a long string of html or text with a slew of double-quotes, then I'm not going to go through and escape a ton of quotes. I just switch to single-quotes around the string declaration and concatenate any php variables into it.

Glad you got it working, nonetheless! :D

CFMaBiSmAd
12-01-2006, 09:26 PM
If your initial post in this thread, #3, had been correct, there would not have been any discussion about it. Post what you mean and mean what you post. People take posted information and run with it, it needs to be accurate.

Tyree
12-01-2006, 09:38 PM
It was accurate. I just didn't realize he had php vars within the string. Looked like a long string of html at first glance. When I realized it had the php in it I fixed it. As you can see, my next post was VERY descriptive and worked perfectly.

So, I did MEAN what I posted. When he had an issue with it he still hadn't done what I suggested because all the escape '\' were still in there. The WORST thing that could've happened if he had followed my suggestion is the $random and $randy vars wouldn't have been parsed. Which was revised with my second post. As I said, I missed that he had vars in the string. So shoot me.

I was trying to be helpful...you don't have to be so condescending.

So, now he still has to escape all the double-quotes. Oh well. Maybe I shouldn't try to help since I may be called out for the simplest of mistakes by the "Mean what you post" police.

mypointofview
12-03-2006, 10:24 PM
Maybe we just had a misunderstanding. My stuff may seem advanced but really, I'm just a photographer, building my site as a beginner. I copy stuff which I find - I don't do websites for others.

So Tyree, what confused me was, when I read your post #3, when there you had single quotes encirculing the html output. For me it did not output the html. So I'm just curious what's going on -- because I like your idea of not having to escape all those quotes.

I looked at your very helpful test page, but I cannot see the PHP behind it. So I tried it now with single quotes around the output and NO backslash before the double quotes inside -- it does NOT work. So howcome you can output stuff which is encircled in single quotes :confused:

Like so:


echo '<a href=\"http://www.codingforums.com\"></a>';


echo '<a href="http://www.codingforums.com"></a>';

Both do not work for me (tested only on PHP 5.14 locally).

The following does however work:


echo "<a href=\"http://www.codingforums.com\"></a>";

--
PS: Thank you Tyree for the alternative to the curly quotes (I have not tested this - however I think the curly quotes are more elegant). Now I will understand them when I see them :thumbsup:

Tyree
12-03-2006, 11:01 PM
Hmmm...your second example there should definitely work. That's exactly what I do.

Just so you know...I'm an artist...not a php programmer. So, don't feel like a little fish in a sea of sharks here. I've been doing websites with an increasing amount of php for a little over a year now. Luckily I have a friend who is an excellent php programmer (as a full-time job) who has taught me an incredible amount! Which kept me from having to beg for answers on boards like this one! ;)

To put it VERY basically, and like the English language, every rule has an exception....but as a rule of thumb...whether you wrap your string with single or double quotes, php is going to consider every character part of the string until it hits another single or double-quote (whichever you started the string with).

CFMaBiSmAd brought up something I hadn't heard before when he said that vars aren't parsed within single-quotes. I don't know if that means they ARE within double-quoted strings. But that's what it seemed like he meant.

When I posted that example page for you, I also posted all the php to go with it here so you could see the code and what the code produced.

Anyway...to your problem...
Have you tried to just put your second example on a page all to itself and see what happens? I'm curious if there's not something else in your code that may be fouling it up.

If your code still looks like it does where you posted it earlier, I can see nothing that would keep it from working. You should be able to cut and paste this:


echo '<a href="javascript:pop_0'.$random.'_horizontal()" title="Click to enlarge" onMouseover = "document.thumb0'.$random.'.src=thumb_0'.$random.'_over.src" onMouseout = "document.thumb0'.$random.'.src=thumb_0'.$random.'.src"><img src="thumbs/thumb_0'.$random.'/thumb_darker.jpg?id='.$randy.'" alt="TheAltText" name="thumb0'.$random.'"></a>';


right into your code in place of your current line and it'd work perfectly.

I'm not using PHP5 on my server yet...but I don't think this is something they've changed. If that were the case, I'm sure GJay and CFMaBiSmAd would have brought that up immediately.

First thing I would do is try the code on a page by itself to see if that works and if it does, we'll know there's something in your file that's causing the problem. Oh and also go ahead and upload it to a test folder on your website so you can see how it will function there. Instead of just testing locally.

It's actually kinda dangerous to test your site on 5.14 locally and upload it to a 4.3.11 server. 5 has features that 4 doesn't!

Let me know!

mypointofview
12-06-2006, 06:39 AM
I think I know what's going on (haven't tested it though because it works now for me). Your single quotes and non-escaping seems to work because you put the variables in dots. That's the difference to my code.

Regarding PHP versions - I installed a package on my Mac and it happened to be the new one. Thanks for the alert - for now the only difference is that I have to set register_globals=on on the older PHP in order for my message forms to work which is high science for me, but heck it works ;-)

Tyree
12-06-2006, 11:40 AM
The dots are what I was talking about when I said, "concatenate the variables into the string." Those dots are used for concatenation.

Yes, you're right. I usually set an .htaccess file in the root of the site to set the register_globals for me. 9 times out of 10 I forget to do that and I get immensely frustrated when my code won't work until the lightbulb goes on and I realize I forgot that! :D

Glad everything's working for you now, bro!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum