Problems doing simple echo [duplicate] - php

This question already has answers here:
Escaping/encoding single quotes in JSON encoded HTML5 data attributes
(2 answers)
Double quotes within php script echo
(6 answers)
Closed 4 years ago.
Well, i'm trying to echo some lines of code but it uses both "" and ''.
As far as I know if you start a echo with "", everytime you use "" it will stop the echo.
How do I print this line of code without breaking it?
This is what I currently have :
echo "<a class='lightbox' href='img/projects/generic/project-16.jpg' data-plugin-options='{'type':'image', 'mainClass': 'mfp-with-zoom', 'zoom': {'enabled': true, 'duration': 300}}'>";
The data-plugin-options='{'type':'image', 'mainClass': 'mfp-with-zoom', 'zoom': {'enabled': true, 'duration': 300}}' is the one that is causing me trouble becouse i cant use "" neither can i use '', becouse one will break the echo, the other one will break the tag.
Hope any of you can help me understand and solve this small problem, big thanks in advance !

Have you tried something like this?
echo "<a class=\"lightbox\" href=\"img/projects/generic/project-16.jpg\" data-plugin-options=\"{'type':'image', 'mainClass': 'mfp-with-zoom', 'zoom': {'enabled': true, 'duration': 300}}\">";
Prefixing double quote in double quoted string with backslash (\") will insert it into the string itself and prevent it from stopping the string block.
Edit: Taking #Barmar's comment into account, it should be more like this to keep data-plugin-options's data as valid JSON, thus enclosing it in single quotes:
echo "<a class=\"lightbox\" href=\"img/projects/generic/project-16.jpg\" data-plugin-options='{\"type\":\"image\", \"mainClass\": \"mfp-with-zoom\", \"zoom\": {\"enabled\": true, \"duration\": 300}}'>";
So to sum it up, PHP does not support escaping double quotes with "", \" needs to be used instead. (analogically \' has to be used for single quotes strings)

The quotes in the data-plugin-options need to be double quotes, since it's parsed as JSON.
You can escape " inside a string delimited with " by preceding it with \.
echo "<a class='lightbox' href='img/projects/generic/project-16.jpg' data-plugin-options='{\"type\":\"image\", \"mainClass\": \"mfp-with-zoom\", \"zoom\": {\"enabled\": true, \"duration\": 300}}'>";

Related

How to escape quotes in a string that assigns a method to onclick with a parameter? [duplicate]

This question already has answers here:
Single and double quotes together as HTML attribute value?
(2 answers)
Closed 3 years ago.
I have a string that contains HTML-Code. And within that string I am assigning a method WITH AN ARGUMENT to the onclick-attribute. This attribute needs to be a string though.
This is the code.
This is how it looks like in HTML.
This is how it should look like in HTML.
I tried to put double quotes before and after, escape them, escape them with multiple backslahes, and many other things, nothing works. All ideas are appreaciated. :-)
This looks like a job for stripslashes(). Note however, that you can not use the same double quote (") or single qoutes (') for both the attribute assignment AND the string definition for the parameter. Therefore you want your result to look something like: onclick="removeTag('naruto')".
If you have them as double quotes in your original string (i.e. $row['description']), you can replace those, all put together like this:
echo 'blabla <a href="#" onclick="removeTag(' . str_replace('"', '\'', stripslashes($row['description'])) . ')">';
A tiny bit simpler approach might be to just trim double quotes and add your own single qoutes:
echo 'blabla <a href="#" onclick="removeTag(\'' . trim(stripslashes($row['description']), '"') . '\')">';

How to fix double/single quotes in PHP [duplicate]

This question already has answers here:
How to work with double quotes and single quotes in php [duplicate]
(2 answers)
Closed 3 years ago.
this should be an easy question but im finding it hard to get a working answer, i am using Sendgrid php api and it requires that i do the following code in this layout.
in my code i have:
//this part is the sendgrid part:
$email->addContent(
"text/html", "//i need to put the bottom image code into here"
<?php echo '<img src="data:image/png;base64,'.base64_encode($portal['image']).'"width="1024px" height="768px"/>'; ?>
the end result should look something like:
$email->addContent(
"text/html", "<img src="data:image/png;base64,'.base64_encode($portal['image']).'"width="1024px" height="768px"/>"
i cant figure out how to use the correct quotes so that this code will work since it uses double and single quotes inside of eachother
You have to match the starting and ending quotes for a string. You're starting the first string with ", but then ending it with ' before the concatenation ..
If you want to put double quotes inside the string, use single quotes around it, and vice versa. If you need to include the same kind of quote inside the string as you use two create it, you have to escape the inner one.
$email->addContent(
"text/html", '<img src="data:image/png;base64,'.base64_encode($portal['image']).'" width="1024px" height="768px"/>'

Using html inside double quoted echos [duplicate]

This question already has answers here:
Double quotes within php script echo
(6 answers)
Echo double quotes inside double quotes issue [duplicate]
(2 answers)
PHP quotes inside quotes
(3 answers)
How to use single quote inside an echo which is using single quote
(7 answers)
Closed 3 years ago.
I want to change the colour of some text and it has to be inside double quotation marks as it is a variable. This is the only instance in which it happens so I won't bother with CSS.
echo "$playername ";
I tried
echo "<font color="yellow"> $playername </font>" ;
But it is not working, I assume this is because it does not like double quotes inside double quotes. Any help would be much appreciated.
The necessity of quoted attribute values only comes into play when dealing with values that may contain contain whitespace or characters meaningful to HTML, such as "<". So, in this case, the OP does not need to quote the attribute value; see here.
If one insists on quoting an attribute value, one may use single quotes or else quote double quotes as in below example:
<?php
$playername = "Nobody";
$yellow="\"yellow\"";
echo "<font color=$yellow>$playername</font>";
Note: while the FONT tag is obsolete, it nonetheless works at any rate in my version of Google Chrome (YMMV). Note, the MDN adamantly discourages using the FONT tag:
Do not use this element! Though once normalized in HTML 3.2, it was
deprecated in HTML 4.01, at the same time as all elements related to
styling only, then obsoleted in HTML5.

Why I can't use single quote inside another single qoute in a PHP variable? [duplicate]

This question already has answers here:
How to use single quote inside an echo which is using single quote
(7 answers)
Closed 5 years ago.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
I have created a PHP variable:
$msg_gifted='<center><h1>It's a gift! </h1>';
If I leave it like this the code is breaking.
When I replace the apostrophe with:
$msg_gifted='<center><h1>It is a gift! </h1>';
everything works fine.
Is there a known way to solve this?
you can do
$msg_gifted="<center><h1>It's a gift! </h1>";
or
$msg_gifted='<center><h1>It\'s a gift! </h1>';
Updated answer
You can not use the same quote inside the string without escaping the quote. So you either use different quotes surrounding the string or you escape it with \. Simplified your code to show different possibilites which will all have the same output.
String in single ' quotes
$msg = '<h1>It\'s a gift!</h1><img src="http://path-to-image.jpg" />';
which is almost the same as tring in double " quotes
$msg = "<h1>It's a gift!</h1><img src=\"http://path-to-image.jpg\" />";
Or string concatenation with different quotes just as you prefer:
$msg = "<h1>It's a gift!</h1>";
$msg.= '<img src="http://path-to-image.jpg" />';
Put \ like this way:
$msg_gifted='<center><h1>It\'s a gift! </h1>';
This is called escaping ' in string in php.

How to work with double quotes and single quotes in php [duplicate]

This question already has answers here:
Escaping quotation marks in PHP
(7 answers)
Closed 5 years ago.
I just wonder how people work with double and single quotes. I have a code where i got a little problem.
If i for example got this code:
<img src="img_fjords.jpg" onclick="document.getElementById('someID').style.display='block'" class="w3-hover-opacity">
Now i got no problem, but if im going to put this inside a echo "" then i start getting problems, so i changed all the double quotes to single quotes and used double quotes for the echo, but still it will not accept it, i think i got to many single quotes now. and i think i need to do something different with "someID"?
echo code:
echo "<img src='img_fjords.jpg' onclick='document.getElementById('someID').style.display='block'' class='w3-hover-opacity'";
You have to escape double quotes if your string is put in double quotes by using backslash \ character. Here is an example:
echo "String with \"double quotes\"";
So in your case it will be:
echo "<img src=\"img_fjords.jpg\" onclick=\"document.getElementById('someID').style.display='block'\" class=\"w3-hover-opacity\"";
Ref: Double quotes within php script echo
You can escape the quotes where needed (using backslashes before the quote signs):
echo "<img src='img_fjords.jpg' onclick='document.getElementById(\"someID\").style.display=\"block\"' class='w3-hover-opacity'";

Categories