Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    mypointofview
    Guest

    echo html code - how to escape underscore

    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 Code:
    <?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.
    Last edited by mypointofview; 12-01-2006 at 02:46 AM. Reason: More accurate spelling

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,747
    Thanks
    18
    Thanked 155 Times in 146 Posts
    You don;t need to escape underscores in PHP or javascript.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Heck, you can echo that line without escaping anything...just do it like this:
    PHP Code:
    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.

  • #4
    mypointofview
    Guest
    PHP Code:
    <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 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

    PHP Code:
    <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 Code:
    <?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 Code:
    <?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>
    Last edited by mypointofview; 12-01-2006 at 07:20 AM.

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,961
    Thanks
    2
    Thanked 305 Times in 297 Posts
    Pretty simple, put the variable in {...} when it is in side of a double-quoted string - {$random}
    PHP Code:
    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.
    Last edited by CFMaBiSmAd; 12-01-2006 at 07:57 AM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 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.
    Last edited by Tyree; 12-01-2006 at 01:25 PM.

  • #7
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    variables are not parsed within single quoted strings
    PHP Code:
    $test='hello';
    echo 
    '$test';
    //compared with
    echo "$test"
    will output:
    Code:
    $testhello

  • #8
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #9
    mypointofview
    Guest
    CFMaBiSmAd, your trick with replacing $random with {$random} works great. Thank you.

    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.
    Last edited by mypointofview; 12-01-2006 at 08:51 PM. Reason: Typos

  • #10
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,961
    Thanks
    2
    Thanked 305 Times in 297 Posts
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #11
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mypointofview View Post
    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:
    PHP Code:
    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!
    Last edited by Tyree; 12-01-2006 at 09:12 PM.

  • #12
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,961
    Thanks
    2
    Thanked 305 Times in 297 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #13
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by Tyree; 12-01-2006 at 09:49 PM.

  • #14
    mypointofview
    Guest
    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

    Like so:

    PHP Code:
    echo '<a href=\"http://www.codingforums.com\"></a>'
    PHP Code:
    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:
    PHP Code:
    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
    Last edited by mypointofview; 12-03-2006 at 11:01 PM. Reason: Semantic clearity

  • #15
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
    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!
    Last edited by Tyree; 12-03-2006 at 11:04 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •