将jQuery / HTML分配给PHP变量

时间:2013-12-25 07:25:52

标签: php jquery html

我在这里有这个脚本。

<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>

现在我希望将它连接到像这样的PHP变量..

$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>'

所以我知道这显然不起作用,因为HTML代码中间还有其他'所以我怎么去做这个没有太多麻烦,因为我看到很多{{1这些。

2 个答案:

答案 0 :(得分:1)

尝试PHP heredoc或nowdoc语法。

定界符:

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

变量在heredoc字符串中被替换,如双引号。

Nowdoc,就变量插值而言,相当于单引号,看起来像

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

有关这些字符串赋值样式的详细信息,请参阅http://www.php.net/manual/en/language.types.string.php

这是heredoc的例子。

<?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);

运行此文件会产生

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>"

答案 1 :(得分:0)

尝试使用addslashes函数:

$new_more_html_content = addslashes($more_html_content);

http://www.php.net/addslashes了解更多信息。