PHP escaping qoutes - php

Just a simple line:
print "<li class=\"productImage\"style=\"background-image: url(\"images/products/1.jpg\");\">";
And the output is:
<li 1.jpg");"="" products="" images="" style="background-image: url(" class="productImage"><img src="images/products/2.jpg"></li>
What I am doing wrong?
Fixed:
print '<li class="productImage" style="background-image: url(\'images/products/1.jpg\');">';

Try with proper escape sequences, using same quotes are misjudged by browser (style in your case):
print "<li class='productImage' style='background-image: url(\"images/products/1.jpg\");'>";

Related

Escape quotes on onclick metho

I have click method generated by php echo. It does not render as it should be. It shows as in the attached image.
my code is
echo "<div class='col-sm-3' onclick='ViewItem('".$item['item_id']."')' style='cursor:pointer'>";
how can I escape the quotes to get the following
<div class='col-sm-3' onclick='ViewItem("1")' style='cursor:pointer'>
We have all been there looking at code too long.
$item['item_id'] = '6';
echo "<div class='col-sm-3' onclick='ViewItem(\"".$item['item_id']."\")' style='cursor:pointer'>";
Output:
<div class='col-sm-3' onclick='ViewItem("6")' style='cursor:pointer'>

How to escape quotes of array keys inside background-image url?

This is my code :
echo '<div class="banner" style="background-image: url(<?php echo $img_Array["sizes"]["large"]; ?>);">';
If I do
echo $img_Array["sizes"]["large"];
I get correct image path but when I use it inside background-image it does not worked for me.
When I used inspect element it displayed:
element.style {
background-image: url(<?php echo $img_Array[;
}
That means it needs to escape quotes(") of sizes , i tried to use /" but didnt work .
Any help would be appreciated . Thank you .
Try this..
echo '<div class="banner" style="background-image: url(' . $img_Array["sizes"]["large"] . ' );">';
When you echo, you are already in PHP. No need to use <?php again.
echo '<div class="banner" style="background-image: url(<?php echo $img_Array["sizes"]["large"]; ?>);">';
Change it to:
echo '<div class="banner" style="background-image: url(' . $img_Array["sizes"]["large"] . ');">';
It seems like you've tried to nest PHP code blocks. There are 2 possible solutions:
Echo the entire HTML code from the PHP block:
<?php
echo '<div class="banner" style="background-image: url('
. $img_Array["sizes"]["large"] . ');">';`
?>
Echo just the URL with a PHP block nested in HTML:
<div class="banner" style="background-image: url('<?php echo $img_Array["sizes"]["large"]; ?>');">

PHP echo inline CSS background url

I am trying to echo some inline CSS using PHP using this:
echo '<div class="image" style="background:url("img/testimage.jpg");width:300px;height:232px;">';
echo '</div>';
But for some reason this is returning this:
<div class="image" testimage.jpg");width:300px;height:232px;"="" img="" style="background:url("></div>
This is within a WordPress environment, am I doing something obvious wrong?
Escape the quotes inside the url declaration properly:
echo '<div class="image" style="background:url(\'img/testimage.jpg\'); width:300px; height:232px;">';
// ^ ^
you can't run something like this (in HTML) correctly:
style="background:url("img/testimage.jpg");width:300px;height:232px;"
must merge between single and duple quotes or escape them:
style="background:url('img/testimage.jpg');width:300px;height:232px;"
solution:
echo "<div class='image' style='background:url(\"img/testimage.jpg\");width:300px;height:232px;'></div>";
echo '<div class="image" style="background:url(\'img/testimage.jpg\');width:300px;height:232px;">';
echo '</div>';

simple Echo Error

i try to make a while{ part in php, to read out a mysql db. Works perfectly, only with the output i have problems:
echo "<div id=\"content\"><ul class=\"pageitem\"><li class=\"store\"><span class=\"image\" style=\"background-image: url('<?php echo \". $zeile['image'] . \"; ?>')\"></span><span class=\"name\"><?php echo \". $zeile['title'] . \"; ?></span><span class=\"arrow\"></span></li></ul>";
Error is:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\xampplite\htdocs\harti\products.php on line 40
Sry i'm really new with php, i think this is a simple mistake but cannot find it.
thx for help
Don't use <?php again...
echo "<div id=\"content\"><ul class=\"pageitem\"><li class=\"store\"><span class=\"image\" style=\"background-image: url('". $zeile['image'] . "')\"></span><span class=\"name\">". $zeile['title'] . "</span><span class=\"arrow\"></span></li></ul></div>";
Also, you forgot to close the DIV.
Instead, you could do something like this (outside <?php and ?>):
<div id="content">
<ul class="pageitem">
<li class="store">
<a href="index.html">
<span class="image" style="background-image: url('<?php echo $zeile['image']; ?>')"></span>
<span class="name"><?php echo $zeile['title']; ?></span>
<span class="arrow"></span>
</a>
</li>
</ul>
</div>
Read up on how strings work in PHP:
http://php.net/manual/en/language.types.string.php
If you use double quotes you can do this:
echo "<b>{$myarray[$index]}</b>";
Or you can do this:
echo 'blah';
Note how I used single quotes to avoid having to escape the double quotes. However, with a single quote, I cannot embed the variable within the quote.
Try this
echo "<div id='content'><ul class='pageitem'><li class='store'><a href='index.html'><span class='image' style='background-image: url(" . $zeile['image'] . ")'></span><span class='name'>" . $zeile['title'] . "</span><span class='arrow'></span></a></li></ul></div>";
Do not use
try this:
echo "<div id=\'content\'><ul class=\'pageitem\'><li class=\'store\'><a href=\'index.html\'><span class=\'image\' style=\'background-image: url(".$zeile['image'].")></span><span class=\'name\'>".$zeile['title']."</span><span class=arrow></span></a></li></ul></div>";
Check Single quotes also.
And even I agree with "Parkyprg", use html content outside php.
you may replace each " with "+"\""+".
It works in all scripting languages.

echo line returns in html source code

How do you implement line returns when the html code is between simple quotes. The only way I found is to concatenate the line return between double quotes:
echo '<div>'."\n"
.'<h3>stuff</h3>'."\n"
.'</div>'."\n";
Which lucks ugly to me.
Edit: The code between the simple quotes is quite long and with many attributes otherwise I would just use double quotes.
echo "<div>\n" .
"<h3>stuff</h3>\n" .
"</div>\n";
or
echo "<div>
<h3>stuff</h3>
</div>\n";
or
echo <<< HTML
<div>
<h3>stuff</h3>
</div>
HTML;
But this is completely subjective.
You can do
echo "<div>\n<h3>stuff</h3>\n</div>\n";
or
echo '<div>
<h3>stuff</h3>
</div>
';
Concatenation: echo '<div>' . "n";
Concatenation with constant: echo '<div>' . PHP_EOL;
Hard line feeds:
echo '<div>
';
End PHP mode: echo '<div>'; ?>
Double quotes: echo "<div>\n";
Heredoc:
echo <<<EOM
<div>
EOM;
Single quotes inhibit backslash sequences. But newlines are pretty much optional outside of CDATA sections, so feel free to omit them.
echo <<<EOM
<div>
<h3 class="blabla" style='booboo' >stuff<h3>
</div>
EOM;
Using the <<< quotation, you don't need to escape any characters(they are simply outputted exactly how they are typed.

Categories