0

I've got this script here.

<li><script id="sid0020000048947519509">(function() {function async_load(){s.id="cid0020000048947519509";s.src='http://st.chatango.com/js/gz/emb.js';s.style.cssText="width:299px;height:433px;";s.async=true;s.text='{"handle":"phanime","arch":"js","styles":{"a":"404040","b":100,"c":"FFFFFF","d":"FFFFFF","k":"404040","l":"404040","m":"404040","n":"FFFFFF","q":"404040","r":100,"t":0,"cv":1,"cvfnt":"Calibri, Candara, Segoe, \'Segoe UI\', Optima, Arial, sans-serif, sans-serif","cvbg":"404040","cvfg":"ffffff","cvh":26,"surl":0}}';var ss = document.getElementsByTagName('script');for (var i=0, l=ss.length; i < l; i++){if (ss[i].id=='sid0020000048947519509'){ss[i].id +='_';ss[i].parentNode.insertBefore(s, ss[i]);break;}}}var s=document.createElement('script');if (s.async==undefined){if (window.addEventListener) {addEventListener('load',async_load,false);}else if (window.attachEvent) {attachEvent('onload',async_load);}}else {async_load();}})();</script></li>

Now I want this to be concatenated to a PHP variable like so..

$more_html_content .= '<li><script id="sid0020000048947519509">(function() {function async_load(){s.id="cid0020000048947519509";s.src='http://st.chatango.com/js/gz/emb.js';s.style.cssText="width:299px;height:433px;";s.async=true;s.text='{"handle":"phanime","arch":"js","styles":{"a":"404040","b":100,"c":"FFFFFF","d":"FFFFFF","k":"404040","l":"404040","m":"404040","n":"FFFFFF","q":"404040","r":100,"t":0,"cv":1,"cvfnt":"Calibri, Candara, Segoe, \'Segoe UI\', Optima, Arial, sans-serif, sans-serif","cvbg":"404040","cvfg":"ffffff","cvh":26,"surl":0}}';var ss = document.getElementsByTagName('script');for (var i=0, l=ss.length; i < l; i++){if (ss[i].id=='sid0020000048947519509'){ss[i].id +='_';ss[i].parentNode.insertBefore(s, ss[i]);break;}}}var s=document.createElement('script');if (s.async==undefined){if (window.addEventListener) {addEventListener('load',async_load,false);}else if (window.attachEvent) {attachEvent('onload',async_load);}}else {async_load();}})();</script></li>'

So I know this obviously won't work since there are other ' in the middle of the HTML code so how do I go about doing this without much of a hassle since I see a lot of ' these.

2
  • Too bad php doesn't have triple quotes like Python! That solves this pretty easily.
    – JAL
    Commented Dec 25, 2013 at 19:18
  • It would, and escaping all these quotes is going to be way too much work.
    – Maaz
    Commented Dec 25, 2013 at 19:22

2 Answers 2

1

Try PHP heredoc or nowdoc syntax.

Heredoc:

$html= <<<EOT
"WHATEVER 'QUOTES' YOU WANT!"
And $variables are interpolated.
EOT;

Variables are replaced within heredoc strings like double quotes.

Nowdoc, the equivalent of single quotes as far as variable interpolation, looks like

$html=<<<'EOT'
Stuff "like" 'whatever' but variables aren't processed in this nowdoc
EOT;

See http://www.php.net/manual/en/language.types.string.php for more info on these styles of string assignment.

Here's heredoc example.

<?php
$more_html_content=<<<EOT
<li><script id="sid0020000048947519509">(function() {function async_load(){s.id="cid0020000048947519509";s.src='http://st.chatango.com/js/gz/emb.js';s.style.cssText="width:299px;height:433px;";s.async=true;s.text='{"handle":"phanime","arch":"js","styles":{"a":"404040","b":100,"c":"FFFFFF","d":"FFFFFF","k":"404040","l":"404040","m":"404040","n":"FFFFFF","q":"404040","r":100,"t":0,"cv":1,"cvfnt":"Calibri, Candara, Segoe, \'Segoe UI\', Optima, Arial, sans-serif, sans-serif","cvbg":"404040","cvfg":"ffffff","cvh":26,"surl":0}}';var ss = document.getElementsByTagName('script');for (var i=0, l=ss.length; i < l; i++){if (ss[i].id=='sid0020000048947519509'){ss[i].id +='_';ss[i].parentNode.insertBefore(s, ss[i]);break;}}}var s=document.createElement('script');if (s.async==undefined){if (window.addEventListener) {addEventListener('load',async_load,false);}else if (window.attachEvent) {attachEvent('onload',async_load);}}else {async_load();}})();</script></li>
EOT;

var_dump($more_html_content);

Running this file produces

MacBook-Pro:~ squiddle$ php -f cow.php 
string(961) "<li><script id="sid0020000048947519509">(function() {function async_load(){s.id="cid0020000048947519509";s.src='http://st.chatango.com/js/gz/emb.js';s.style.cssText="width:299px;height:433px;";s.async=true;s.text='{"handle":"phanime","arch":"js","styles":{"a":"404040","b":100,"c":"FFFFFF","d":"FFFFFF","k":"404040","l":"404040","m":"404040","n":"FFFFFF","q":"404040","r":100,"t":0,"cv":1,"cvfnt":"Calibri, Candara, Segoe, \'Segoe UI\', Optima, Arial, sans-serif, sans-serif","cvbg":"404040","cvfg":"ffffff","cvh":26,"surl":0}}';var ss = document.getElementsByTagName('script');for (var i=0, l=ss.length; i < l; i++){if (ss[i].id=='sid0020000048947519509'){ss[i].id +='_';ss[i].parentNode.insertBefore(s, ss[i]);break;}}}var s=document.createElement('script');if (s.async==undefined){if (window.addEventListener) {addEventListener('load',async_load,false);}else if (window.attachEvent) {attachEvent('onload',async_load);}}else {async_load();}})();</script></li>"
4
  • what's the difference between using EOD and EOT? By the way.. Here is what I am trying to do.. which doesn't seem to work. pastebin.com/S3iZW82S
    – Maaz
    Commented Dec 25, 2013 at 9:46
  • @Maaz the end identifier can be anything you'd like, actually. It's doesn't have to be EOT or EOD (i think those mean end of text and end of data).
    – JAL
    Commented Dec 25, 2013 at 11:05
  • @Maaz you can't indent the end identifier. From the docs, "Warning It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline."
    – JAL
    Commented Dec 25, 2013 at 11:07
  • could you possibly give me an example of how that would look with this code?
    – Maaz
    Commented Dec 25, 2013 at 19:10
0

Try the addslashes function:

$new_more_html_content = addslashes($more_html_content);

http://www.php.net/addslashes for more information.

6
  • How would that work exactly, wouldn't $more_html_content string not have the whole content since there will be " characters in the middle of the string that will end the string early? For example this code here pastebin.com/0cVQsyYg
    – Maaz
    Commented Dec 25, 2013 at 7:40
  • Right, then what about running a also a str_replace()? That should solve the prob. $new_more_html_content = addslashes(""","",$more_html_content)); Commented Dec 25, 2013 at 7:46
  • wouldn't $more_html_content not have the whole intended string from the get go?
    – Maaz
    Commented Dec 25, 2013 at 8:19
  • Right. It would be something like $new_more_html_content = str_replace(""","",addslashes($more_html_content)); Commented Dec 25, 2013 at 17:37
  • Have you personally tried this? It's not working for me at all.
    – Maaz
    Commented Dec 25, 2013 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.