0

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 !

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Escaping quotes in PHP string context is done with backslashes `\"`. And for HTML context you should apply [`htmlspecialchars`](http://php.net/htmlspecialchars), perhaps even use [`json_encode`](http://php.net/json_encode) rather than a literal. – mario Nov 17 '18 at 15:56

2 Answers2

1

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)

p0358
  • 139
  • 1
  • 8
  • "Try this" does not make for a good answer. You should explain *how* and *why* this solves their problem. I recommend reading, "[How do I write a good answer?"](http://stackoverflow.com/help/how-to-answer) – John Conde Nov 17 '18 at 15:56
  • The value of `data-plugin-options` needs to be JSON, which only allows double quotes around strings. – Barmar Nov 17 '18 at 15:57
  • @JohnConde Thanks for your comment, I have edited my answer adding a short explaination. – p0358 Nov 17 '18 at 15:58
  • Thank all of you ! Not only was I able to fix my problem, i got to the root of it and understood it ! Major thanks to all of you ! – João Fernandes Nov 17 '18 at 16:11
0

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}}'>";
Barmar
  • 741,623
  • 53
  • 500
  • 612