从 PHP 回显 javascript

发布于 2024-11-14 11:26:46 字数 928 浏览 2 评论 0原文

我试图从 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&amp;label=ffr456dj5QIQ7YzN2wM&amp;guid=ON&amp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

牵强ㄟ 2024-11-21 11:26:46
echo <<< EOD

    your html here

EOD;
echo <<< EOD

    your html here

EOD;
才能让你更想念 2024-11-21 11:26:46

它喜欢使用 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.

○闲身 2024-11-21 11:26:46

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

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

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

明显工作(如果您查看源代码):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:

$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&label=ffr456dj5QIQ7YzN2wM&guid=ON&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.

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

葵雨 2024-11-21 11:26:46

有什么问题:

<?php
$JS='
<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&label=ffr456dj5QIQ7YzN2wM&guid=ON&script=0"/>
</div>
</noscript>';

What's wrong with:

<?php
$JS='
<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&label=ffr456dj5QIQ7YzN2wM&guid=ON&script=0"/>
</div>
</noscript>';
李白 2024-11-21 11:26:46

好吧,你没有确切地说出什么不起作用。带引号的 /* 不应被视为注释。如果这没有给你带来你所期望的结果,那么你的代码中其他地方可能有不匹配的引号。乍一看,我发现:

$sJS .='/* <![CDATA[ */"';

看起来您有 " ,然后是 ' 最后。您可能不需要 "

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:

$sJS .='/* <![CDATA[ */"';

It looks like you have " and then ' at the end. You probably don't need the "

薄荷梦 2024-11-21 11:26:46

您可以使用 Heredoc Nowdoc 语法可以更轻松地完成此任务。

Nowdoc(在 php > 5.3 中)将不会解析变量(类似于单引号)

Heredoc 解决方案:

echo <<< EOD

<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&label=ffr456dj5QIQ7YzN2wM&guid=ON&script=0"/>
</div>
</noscript>

EOD;

对于 nowdoc,唯一的区别是将 use EOT 带单引号('EOT'):

echo <<<'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:

echo <<< EOD

<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&label=ffr456dj5QIQ7YzN2wM&guid=ON&script=0"/>
</div>
</noscript>

EOD;

For nowdoc, the only difference would be to put the use EOT with single quotes ('EOT'):

echo <<<'EOT'
...
EOT;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文