在 PHP 中回显 Javascript 时需要转义哪些字符?
我正在尝试在 PHP if 语句中回显 Google AdWords 转化跟踪代码的 Javascript。我已经进行了大量的谷歌搜索,但似乎无法找到我需要转义哪些字符才能正确执行代码的明确列表。有人有建议吗?
第一段代码是 if 语句的开头,该语句调用包含转换代码片段的内容:
if ( @mailit ) {
include ("conversioncodes.php");
这是 conversioncodes.php 的内容,其中包含我试图弄清楚如何正确转义的代码片段。目标是让转换代码在满足 if 语句的条件时跟踪转换。
echo "<!-- Google Code for Homepage Form Submit Conversion Page -->
<script type="text/javascript">
<!--
var google_conversion_id = XXXXXXXXXX;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "XXXXXXXXXXXXXXX";
var google_conversion_value = 0;
//-->
</script>
<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/XXXXXXXXXX/?label=XXXXXXXXXXXXXXXXXXX&guid=ON&script=0"/>
</div>
</noscript>";
I'm trying to echo out the Javascript for the Google AdWords conversion tracking code within a PHP if statement. I've done a bunch of googling but can't seem to find a definitive list of what characters I'd need to escape to have the code execute properly. Anybody have suggestions?
This first bit of code is the beginning of the if statement that calls an include with the conversion code snippet:
if ( @mailit ) {
include ("conversioncodes.php");
This is the contents of conversioncodes.php that includes the code snippet that I'm trying to figure out how to escape properly. The goal is to have the conversion code track the conversion when the conditions of the if statement is met.
echo "<!-- Google Code for Homepage Form Submit Conversion Page -->
<script type="text/javascript">
<!--
var google_conversion_id = XXXXXXXXXX;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "XXXXXXXXXXXXXXX";
var google_conversion_value = 0;
//-->
</script>
<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/XXXXXXXXXX/?label=XXXXXXXXXXXXXXXXXXX&guid=ON&script=0"/>
</div>
</noscript>";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您使用的是 PHP 5.3+,只需使用 NOWDOC 并且不会进行任何解析,因此您不必转义任何内容:
Assuming you're using PHP 5.3+, just use a NOWDOC and no parsing will be done, so you won't have to escape anything:
只需使用单引号即可。
关闭:单引号比双引号快几倍,我所说的几倍是指很多。
Just use single quotes instead.
Off: Single quotes are several times faster than double quotes, and by several I mean a LOT.
也许只是脱离了php?这样,您还可以使语法突出显示尽可能清晰。
作为旁注,请记住,您在这里真正缺少的元素是提供某种模板的强大方法。模板使 php 的处理变得更加清晰,将表示逻辑与业务逻辑分开。如果不使用完整的模板引擎,我强烈建议至少使用单独的 php 文件作为模板。
Perhaps just break out of php? This way you'll also keep the syntax highlighting as clear as you can.
Just as a side-note, keep in mind that the element that you're really missing here is a robust way of providing some kind of template. Templates make dealing with php so much cleaner, separates the presentation logic from the business logic. I highly recommend at least using separated php files as templates, if not using a full blown template engine.
不久前我对同一个问题感到困惑。一般的答案确实不是微不足道的。
我放弃并重写了我的代码以使用 json_encode() ,这非常容易地解决了所有问题。而且重写也很快。如果我早点想到的话,我就能节省很多时间。
将您需要提供给 JS 脚本的数据打包在一个对象或数组中,
json_encode()
将其写入。I got all confused on the same problem a while ago. The general answer is really not trivial.
I gave up and rewrote my code to use
json_encode()
which sorted it all out very easily. And the rewrite was quick too. I'd have saved myself a lot of time had I thought of it sooner.Package up in an object or array the data you need give to your JS script,
json_encode()
it and write that.