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

dmoneyman

New Member
Messages
19
Reaction score
0
Points
0
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:

freecrm

New Member
Messages
629
Reaction score
0
Points
0
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:
$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:
$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;
 

dmoneyman

New Member
Messages
19
Reaction score
0
Points
0
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:
[COLOR=#000000][FONT=Courier New][COLOR=#0000bb]$images[/COLOR][COLOR=#007700]=[/COLOR][/FONT][FONT=Courier New][COLOR=#dd0000]" [/COLOR][/FONT]
[FONT=Courier New][COLOR=#dd0000]<table> [/COLOR][/FONT]
[FONT=Courier New][COLOR=#dd0000]<tr> [/COLOR][/FONT]
[FONT=Courier New][COLOR=#dd0000]           <td><a href><img src='http://example.com/image1.gif'></a>[FONT=Courier New][COLOR=#dd0000]</td>  [/COLOR][/FONT][/COLOR][/FONT]
[FONT=Courier New][COLOR=#dd0000]           <td><a href><img src="http://example.com/image2.gif"></a>[/COLOR][/FONT][FONT=Courier New][COLOR=#dd0000]</td>  [/COLOR][/FONT]
 
[FONT=Courier New][COLOR=#dd0000]</tr> [/COLOR][/FONT]
[FONT=Courier New][COLOR=#dd0000]</table> [/COLOR][/FONT]
[FONT=Courier New][COLOR=#dd0000]"[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]; [/COLOR][/FONT]
[FONT=Courier New][COLOR=#007700]echo [/COLOR][COLOR=#0000bb]$images[/COLOR][COLOR=#007700]; [/COLOR][/FONT][/COLOR]
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
add the "\" character before the double quotes:
Code:
\"
and for the slahes
Code:
\/\/
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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
 

primefalcon

New Member
Messages
15
Reaction score
0
Points
0
Why Don't you just use the heredoc syntax?

example

PHP:
<?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;
?>
 
Top