简体   繁体   English

php函数来回显javascript字符串

[英]php function to echo javascript string

Is the a proper php function to echo javascript string? 是一个正确的PHP函数来回显javascript字符串?
I want to the php function to echo something like this: 我想php函数回显这样的事情:

<!--/* OpenX Interstitial or Floating DHTML Tag v2.8.7 */-->
<script type="text/javascript">// <![CDATA[
//<![CDATA[
var ox_u = 'extremely_long_url_string';
if (document.context) ox_u += '&context=' + escape(document.context);
document.write("<scr"+"ipt type='text/javascript' src='" + ox_u + "'></scr"+"ipt>");
//
// ]]></script>

I know I can put it all in one line and use \\ to escape all the quotes BUT I'm looking for a more elegant & effective solution. 我知道我可以把它全部放在一行并使用\\来逃避所有的引用但我正在寻找一个更优雅和有效的解决方案。

Use the heredoc-syntax . 使用heredoc语法

Heredoc text behaves just like a double-quoted string, without the double quotes. Heredoc文本的行为就像双引号字符串,没有双引号。 This means that quotes in a heredoc do not need to be escaped […]. 这意味着heredoc中的引号不需要转义[...]。

Use HEREDOC: 使用HEREDOC:

$a=<<<BLA
<!--/* OpenX Interstitial or Floating DHTML Tag v2.8.7 */-->
<script type="text/javascript">// <![CDATA[
//<![CDATA[
var ox_u = 'extremely_long_url_string';
if (document.context) ox_u += '&context=' + escape(document.context);
document.write("<scr"+"ipt type='text/javascript' src='" + ox_u + "'></scr"+"ipt>");
//
// ]]></script>
BLA;

and then you can simply 然后你就可以了

echo $a;

There is nothing specific or only for JavaScript but every output feature works for JavaScript. 没有特定的或仅适用于JavaScript,但每个输出功能都适用于JavaScript。 You can just print the out of <?php ?> tag like 你可以打印出<?php ?>标签之类的

<?php 
session_start(); //just an example PHP code
?>
<!--/* OpenX Interstitial or Floating DHTML Tag v2.8.7 */-->
<script type="text/javascript">// <![CDATA[
//<![CDATA[
var ox_u = 'extremely_long_url_string';
if (document.context) ox_u += '&context=' + escape(document.context);
document.write("<scr"+"ipt type='text/javascript' src='" + ox_u + "'></scr"+"ipt>");
//
// ]]></script>

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

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