简体   繁体   English

从 PHP 呼应 javascript

[英]Echoing javascript from PHP

I am trying to echo some google analytics javascript code from PHP so it can be conditionally read based on specific scenarios.我试图从 PHP 中呼应一些谷歌分析 javascript 代码,以便可以根据特定场景有条件地读取它。 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.我正在寻找将此类文本分配给 php 变量的方向。

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

    your html here

EOD;

It like to use heredoc syntax for that sort of stuff.它喜欢对这类东西使用heredoc语法

Just have your js in a seperate include with the heredoc variable, it makes a much cleaner style.只需将您的 js 与heredoc 变量放在一个单独的包含中,它就会使样式更加简洁。

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

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 "看起来你有"然后'最后。你可能不需要"

You don't need to do it line by line like that... PHP supports continuation to the next line.您不需要像那样逐行执行... PHP 支持继续到下一行。 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.您的字符串用单引号引起来,其中没有单引号,也没有反斜杠或转义序列。

Visibly working (if you view source) at: http://gfosco.kodingen.com/phpjs.php可见工作(如果您查看源代码): http://gfosco.kodingen.com/phpjs.php

You can use either the Heredoc or Nowdoc syntax to accomplish this easier.您可以使用HeredocNowdoc语法来更轻松地完成此操作。

Nowdoc (in php > 5.3) will not parse variables (similarly to single quotes) Nowdoc(在 php > 5.3 中)不会解析变量(类似于单引号)

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

EOD;

For nowdoc, the only difference would be to put the use EOT with single quotes ('EOT'):对于 nowdoc,唯一的区别是将使用 EOT 与单引号 ('EOT'):

echo <<<'EOT'
...
EOT;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM