php 如何在php中回显脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11845159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:15:51  来源:igfitidea点击:

How to echo script in php

php

提问by Joshc

I am using the shortcode execute plugin in wordpress.

我在 wordpress 中使用短代码执行插件。

This simply allows me to write shortcode like this [email_spamproof]

这只是让我写这样的短代码 [email_spamproof]

But I am trying to echo a script. Please see below...

但我试图回应一个脚本。请看下面...

<?php

echo '<script type="text/javascript">
    <!-- 
    // spam protected email
    emailE=("enquiries@" + "example.co.uk")
    document.write('<a title="E-mail Example" href="mailto:' + emailE + '">' + emailE + '</a>')
     //-->
    </script>
    <noscript>
        <span class="spam-protected">Email address protected by JavaScript. Please enable JavaScript.</span>
    </noscript>';

?>



Now you can probably see my problem already.

现在你可能已经看到我的问题了。

Usually when I echo stuff it goes like this... echo 'hello';

通常当我回声时,它是这样的...... echo 'hello';

And you break the string using the same apostrophes - like this...
echo 'hello ' . php_name() . ' and friends'

并且您使用相同的撇号断开字符串 - 就像这样......
echo 'hello ' . php_name() . ' and friends'

My Problem

我的问题

The script also uses a similar method by adding script variables into the string, but these script apostrophes will get confused as PHP apostrophes, and break the echoed string.

该脚本也使用了类似的方法,将脚本变量添加到字符串中,但这些脚本撇号会与 PHP 撇号混淆,并破坏回显的字符串。

How can I avoid this?

我怎样才能避免这种情况?

Thanks

谢谢

回答by Michal Grohman

The right code:

正确的代码:

<?php

echo "<script type='text/javascript'>
    <!-- 
    // spam protected email
    emailE=('enquiries@' + 'example.co.uk')
    document.write('<a title='E-mail Example' href='mailto:' + emailE + ''>' + emailE + '</a>')
     //-->
    </script>
    <noscript>
        <span class='spam-protected'>Email address protected by JavaScript. Please enable JavaScript.</span>
    </noscript>";

?>

回答by Iván Pastor

It is easy. Save your javascript code in a new one file and then use this to load it:

这很容易。将您的 javascript 代码保存在一个新的文件中,然后使用它来加载它:

include('myjavascript.php');

Then you will use the includeoption as a echobecause the web will understand your code as HTML and you execute it when you want with php(like echo).

然后,您将使用该include选项作为 a,echo因为 Web 会将您的代码理解为 HTML,并且您可以在需要时使用php(如echo)执行它。

回答by alex

The \character is the escape character. Prepend one to each internal use of the character used to delimit the string.

\字符是转义字符。在用于分隔字符串的字符的每个内部使用前添加一个。

Alternatively, use a HEREDOC.

或者,使用HEREDOC

回答by Clayton

You could write " in your javasript code, and then only use single quotes(') in your php echo.

您可以在 javasript 代码中编写 ",然后在 php echo 中只使用单引号 (')。

A more suitablemethod is to use escape characters in your javascript code "\'" (without the double quotes)

一个更合适的方法是使用转义字符在JavaScript代码“ \””(不含双引号)

回答by mlishn

You need to escape them and then concatenate the variables :

您需要对它们进行转义,然后连接变量:

(Also I dont know if you realize, but you are echoing a commented out section of javascript)

(另外,我不知道你是否意识到,但你正在回应 javascript 的注释部分)

echo '<script type="text/javascript">    
  <!--      // spam protected email     
  emailE=("enquiries@" + "example.co.uk")     
  document.write(\'<a title="E-mail Example" href="mailto:'.emailE.'">'.emailE.'</a>\')      
  //-->     
  </script>     
  <noscript>         
  <span class="spam-protected">Email address protected by JavaScript. Please enable JavaScript.</span>     
  </noscript>'; 

回答by Mark

You can either escape them with a back slash: echo 'It\'s cold';or use heredoc:

您可以使用反斜杠转义它们:echo 'It\'s cold';或使用heredoc

echo <<<END
lots of text here
END;