从 PHP 回显 javascript
我试图从 PHP 中回显一些谷歌分析 javascript 代码,以便可以根据特定场景有条件地读取它。由于代码包含 /* */ 字符,我很难理解引用。我正在寻找将这种类型的文本分配给 php 变量的方向。
谢谢
$sJS .='<script type="text/javascript">';
$sJS .='/* <![CDATA[ */"';
$sJS .='var google_conversion_language = "en";';
$sJS .='var google_conversion_format = "2";';
$sJS .='var google_conversion_color = "ffffff";';
$sJS .='var google_conversion_value = 0;';
$sJS .='/* ]]> */';
$sJS .='</script>';
$sJS .='<script type="text/javascript" src="http://www.googleadservices.com/page.js">';
$sJS .='</script>';
$sJS .='<noscript>';
$sJS .='<div style="display:inline;">';
$sJS .='<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/997410413/?value=1.00&label=ffr456dj5QIQ7YzN2wM&guid=ON&script=0"/>';
$sJS .='</div>';
$sJS .='</noscript>';
I am trying to echo some google analytics javascript code from PHP so it can be conditionally read based on specific scenarios. I'm having difficulty wrapping my head around the quoting since the code contains /* */ characters. I'm looking for some direction in assigning this type of text to a php variable.
Thanks
$sJS .='<script type="text/javascript">';
$sJS .='/* <![CDATA[ */"';
$sJS .='var google_conversion_language = "en";';
$sJS .='var google_conversion_format = "2";';
$sJS .='var google_conversion_color = "ffffff";';
$sJS .='var google_conversion_value = 0;';
$sJS .='/* ]]> */';
$sJS .='</script>';
$sJS .='<script type="text/javascript" src="http://www.googleadservices.com/page.js">';
$sJS .='</script>';
$sJS .='<noscript>';
$sJS .='<div style="display:inline;">';
$sJS .='<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/997410413/?value=1.00&label=ffr456dj5QIQ7YzN2wM&guid=ON&script=0"/>';
$sJS .='</div>';
$sJS .='</noscript>';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它喜欢使用 heredoc 语法< /a> 之类的东西。
只需将你的 js 与heredoc变量单独包含在一起,它就会产生更简洁的风格。
It like to use heredoc syntax for that sort of stuff.
Just have your js in a seperate include with the heredoc variable, it makes a much cleaner style.
您不需要像这样逐行执行... PHP 支持继续到下一行。这工作得很好:
您的字符串用单引号引起来,其中没有单引号,并且没有反斜杠或转义序列。
明显工作(如果您查看源代码):http://gfosco.kodingen.com/phpjs.php
You don't need to do it line by line like that... PHP supports continuation to the next line. This works just fine:
Your string is quoted with single quotes, there are no single quotes inside of it, and there are no backslashes or escape sequences.
Visibly working (if you view source) at: http://gfosco.kodingen.com/phpjs.php
有什么问题:
What's wrong with:
好吧,你没有确切地说出什么不起作用。带引号的
/*
不应被视为注释。如果这没有给你带来你所期望的结果,那么你的代码中其他地方可能有不匹配的引号。乍一看,我发现:看起来您有
"
,然后是'
最后。您可能不需要"
Well you don't say exactly what's not working. A quoted
/*
shouldn't be treated as a comment. It's likely you have mismatched quotes elsewhere in your code if this isn't giving you what you expect. Just at a glance i see:It looks like you have
"
and then'
at the end. You probably don't need the"
您可以使用 Heredoc 或 Nowdoc 语法可以更轻松地完成此任务。
Nowdoc(在 php > 5.3 中)将不会解析变量(类似于单引号)
Heredoc 解决方案:
对于 nowdoc,唯一的区别是将 use EOT 带单引号('EOT'):
You can use either the Heredoc or Nowdoc syntax to accomplish this easier.
Nowdoc (in php > 5.3) will not parse variables (similarly to single quotes)
Heredoc solution:
For nowdoc, the only difference would be to put the use EOT with single quotes ('EOT'):