+ Reply to Thread
Results 1 to 9 of 9

Thread: Copying HTML tags into PHP script, problem with quotes and //

  1. #1
    dmoneyman is offline x10Hosting Member dmoneyman is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    19

    Copying HTML tags into PHP script, problem with quotes and //

    I am copying over some HTML tags, echo'ing them into a PHP file

    I run into problems because some of the image HTML tags have quotes, or the double slashes in a url and that causes PHP to mess up. Is there a way to be able to read these in?

    For example:

    file1 has contents:
    Code:
    <a href><img src='http://example.com/image.gif'></a>"
    my command is:
    Code:
    echo "`grep \"href\" file1`">> file.php
    I think it's also possible to get http tags in a double quote as well, which throws off my echo command as well. This is a shell scripting and PHP problem.

    file1 has contents (Difference is the double quotes around url instead of single quotes):
    Code:
    <a href><img src="http://example.com/image.gif"></a>"
    my command is:
    Code:
    echo "`grep \"href\" file1`">> file.php
    Any ideas?

    Maybe one way would be to use something different than cat and only pull out the image.gif url. Problem is I don't know how to pull that url only out of the tag. Grep will pull the whole line as well. Sed I don't know how to use.
    Last edited by dmoneyman; 04-02-2009 at 11:09 AM.

  2. #2
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: Copying HTML tags into PHP script, problem with quotes and //

    You can use double or singel quotes in both http and php, provided you don't mix the two.

    For instance, if you want to specify a variable...

    PHP Code:
    $variable="

    <table>
    <tr>
                <td class='calday'>Sun</td>
                <td class='calday'>Mon</td>
                <td class='calday'>Tue</td>
                <td class='calday'>Wed</td>
                <td class='calday'>Thu</td>
                <td class='calday'>Fri</td>
                <td class='calday'>Sat</td>
    </tr>
    </table>
    "
    ;
    echo 
    $variable
    If you start with double quotes (like I have done above) a second double quote will end the continuation, so you just use single quotes wherever you need to. Only at the end of the variable do you close it off with another double quote.

    Alternatively, you could start with a single quote, use double quotes in the variable and then finish off with another single quote.

    If you need to break the variable deliberately, this is simply done by deliberately using double quotes.

    e.g.

    PHP Code:
    $calday="classname";
    $variable="
    <table>
    <tr>
                <td class='"
    .$calday."'>Sun</td>
                <td class='"
    .$calday."'>Mon</td>
                <td class='"
    .$calday."'>Tue</td>
                <td class='"
    .$calday."'>Wed</td>
                <td class='"
    .$calday."'>Thu</td>
                <td class='"
    .$calday."'>Fri</td>
                <td class='"
    .$calday."'>Sat</td>
    </tr>
    </table>
    "
    ;
    echo 
    $variable

  3. #3
    dmoneyman is offline x10Hosting Member dmoneyman is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    19

    Re: Copying HTML tags into PHP script, problem with quotes and //

    The problem is that I have hardcoded a double quote opening, but sometimes the html tag that comes in has either single or double quotes. If it's single quotes, i'm good. But if it's double-quotes, then it will close the hardcoded quotes and cause an error. My problem is that I can't control how the html tag comes in, single or double-quotes. Is there a work around for this or how should I code this?

    Also, the url has http://example.com and the double slash, //, causes PHP to think the rest of the line is a comment.

    Code:
    $images=" 
    <table> 
    <tr> 
               <td><a href><img src='http://example.com/image1.gif'></a></td>  
               <td><a href><img src="http://example.com/image2.gif"></a></td>  
     
    </tr> 
    </table> 
    "; 
    echo $images; 

  4. #4
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Copying HTML tags into PHP script, problem with quotes and //

    add the "\" character before the double quotes:
    Code:
    \"
    and for the slahes
    Code:
    \/\/
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Copying HTML tags into PHP script, problem with quotes and //

    To expand on xav0989's comment, you can use sed from the command line to perform the replace:
    Code:
    echo "`grep \"href\" file1`" | sed -e 's/"/\\"/g' -e 's/\/\//\\\/\\\//g' >> file.php
    You could also use awk or perl if you're more comfortable with those. I picked sed because it's fairly simple to use in this case (mod the need for so many '\').

    I don't quite understand the problem with '//'. Is PHP interpreting '//' as a T_COMMENT even though it's in a string?

  6. #6
    dmoneyman is offline x10Hosting Member dmoneyman is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    19

    Re: Copying HTML tags into PHP script, problem with quotes and //

    Quote Originally Posted by misson View Post

    I don't quite understand the problem with '//'. Is PHP interpreting '//' as a T_COMMENT even though it's in a string?
    Yes, PHP is interpreting the // as a comment.

    Thanks for that sed command, I need to read up on using sed.

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Copying HTML tags into PHP script, problem with quotes and //

    Quote Originally Posted by dmoneyman View Post
    Yes, PHP is interpreting the // as a comment.
    I think I just noticed what's going on. When the double quotes in the <a href="..."> cause the string to end prematurely, the '//' are no longer in a string and are thus interpreted as T_COMMENT. Once you escape the '"', the '//' won't be interpreted as a comment.

    This simplifies the command to:
    Code:
    echo "`grep \"href\" file1`" | sed -e 's/"/\\"/g' >> file.php

  8. #8
    primefalcon is offline x10Hosting Member primefalcon is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    15

    Re: Copying HTML tags into PHP script, problem with quotes and //

    Why Don't you just use the heredoc syntax?

    example

    PHP Code:
    <?php
    echo <<<HEREDOC
    Hell there using the heredoc syntax I can use "'s or 's or whatever and it doesn't have a problem with it.

    The only thing you have to remember is up above where I stated heredoc you can use anything you want, but just use all caps and you have to have the same word or such ending on a line by itself with nothing else except the closing semi-colon like this:
    HEREDOC;

    echo <<<HEREISANOTHEREXAMPLE
    to demonstrate that you can use any word or such look at this example, the only thing you have to remember is it has to be a word that you're not going to use in the echoed portion of your document, though case matters remember "This" is not the same as "thiS"

    Isn't heredoc wonderful? it's meant for just this type of thing
    HEREISANOTHEREXAMPLE;
    ?>

  9. #9
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Copying HTML tags into PHP script, problem with quotes and //

    In your case, nowdoc would be a better idea - if supported (php 5.3). In heredoc, there is some parsing made, just as in double quoted strings, and there is no parsing in nowdoc, just as in single quoted strings.
    The PHP site, tells that nowdoc was developped excatly for your type of problem:
    http://www.php.net/manual/en/languag....syntax.nowdoc
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread

Similar Threads

  1. PHP Problem: gd_version() crashes script
    By GG-Xtreme in forum Free Hosting
    Replies: 2
    Last Post: 08-18-2008, 01:36 PM
  2. Replies: 3
    Last Post: 05-10-2008, 12:05 AM
  3. Replies: 8
    Last Post: 12-03-2007, 04:12 PM
  4. PHP remove html/other uncessary tags? - need help
    By a-a-a in forum Scripts & 3rd Party Apps
    Replies: 8
    Last Post: 10-30-2007, 07:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers