从PHP回显JavaScript [英] Echoing javascript from PHP

查看:72
本文介绍了从PHP回显JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从PHP中回显一些Google Analytics(分析)JavaScript代码,以便可以根据特定情况有条件地对其进行读取。因为代码中包含/ * * /字符,所以我难以理解引号。我正在寻找将这种类型的文本分配给php变量的方向。

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.

谢谢

$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&amp;label=ffr456dj5QIQ7YzN2wM&amp;guid=ON&amp;script=0"/>';
$sJS .='</div>';
$sJS .='</noscript>';


推荐答案

您不需要像这样逐行执行... PHP支持继续到下一行。效果很好:

You don't need to do it line by line like that... PHP supports continuation to the next line. This works just fine:

$sJS = '<script type="text/javascript">
    /* <![CDATA[ */
    var google_conversion_language = "en";
    var google_conversion_format = "2";
    var google_conversion_color = "ffffff";
    var google_conversion_value = 0;
    /* ]]> */
    </script>
    <script type="text/javascript" src="http://www.googleadservices.com/page.js">
    </script>
    <noscript>
    <div style="display:inline;">
    <img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/997410413/?value=1.00&amp;label=ffr456dj5QIQ7YzN2wM&amp;guid=ON&amp;script=0"/>
    </div>
    </noscript>';

您的字符串用单引号引起来,其中没有单引号,也没有反斜杠或转义序列。

Your string is quoted with single quotes, there are no single quotes inside of it, and there are no backslashes or escape sequences.

可见(如果您查看源代码),位于: http://gfosco.kodingen.com/phpjs.php

Visibly working (if you view source) at: http://gfosco.kodingen.com/phpjs.php

这篇关于从PHP回显JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆