PHP 生成的 javascript 和引号

发布于 2024-08-11 16:14:43 字数 509 浏览 2 评论 0原文

我在 PHP 代码中生成一些 javascript,并且需要将一些 php 变量分配给 javascript 变量。不幸的是,有时我的 PHP 变量包含引号。例如:

$foo = "'Dis here be a \"string\"";
print "<script type='text/javascript'>var foo = '{$foo}';</script>";

将生成一个 javascript 错误,因为生成的 javascript 将如下所示:

<script type='text/javascript'>var foo = '"'Dis here be a \"string\"';

我知道我可以在 $foo 上使用 regexp 将所有 ' 标记替换为 \' 但这由于各种原因很难。除此之外我还能做些什么吗?类似于 perl q() 函数的东西......

I'm generating some javascript in my PHP code, and I need to assign some php variables to javascript variables. Unfortunately, sometimes my PHP variables contain quote marks. for instance:

$foo = "'Dis here be a \"string\"";
print "<script type='text/javascript'>var foo = '{$foo}';</script>";

will generate a javascript error because the resulting javascript will look like this:

<script type='text/javascript'>var foo = '"'Dis here be a \"string\"';

I know I can use regexp on $foo to replace all ' marks with \' but this is hard for various reasons. Is there anything I can do short of that? Something akin to the perl q() function...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

暮色兮凉城 2024-08-18 16:14:43

尝试这样做吗?

$foo = "'Dis here be a \"string\"";
echo '<script type="text/javascript">var foo = "'.addslashes($foo).'";</script>';

请参阅:http://php.net/manual/en/function.addslashes.php

Tried doing this?

$foo = "'Dis here be a \"string\"";
echo '<script type="text/javascript">var foo = "'.addslashes($foo).'";</script>';

See: http://php.net/manual/en/function.addslashes.php

独守阴晴ぅ圆缺 2024-08-18 16:14:43

这应该是朝着正确方向迈出的一步:

addcslashes($str, "\"\r\n\\\t/\0..\37");

This should be a step in the right direction:

addcslashes($str, "\"\r\n\\\t/\0..\37");
甩你一脸翔 2024-08-18 16:14:43

你确定吗?不是吗:

var foo = ''Dis here be a "string"'

为了防止双'尝试:

$foo = "\'Dis here be a \"string\"";

或者

$foo = '\\\'Dis here be a "string"';

Are you sure? Isn't it:

var foo = ''Dis here be a "string"'

In order to prevent the double ' try:

$foo = "\'Dis here be a \"string\"";

or

$foo = '\\\'Dis here be a "string"';
芯好空 2024-08-18 16:14:43

还值得注意的是,您可以将 PHP 文件用作 JavaScript 文件

<script type="text/javascript" src="js/main.php"></script>

,并且您可以在该文件中执行 PHP 代码,也可以通过 PHP 的回显来输出 JavaScript 代码。

It's also worth noting that you can use a PHP file as a JavaScript file

<script type="text/javascript" src="js/main.php"></script>

And you're able to execute PHP code in that file, as well as output JavaScript code by echoing from PHP.

猛虎独行 2024-08-18 16:14:43

由于您在 JavaScript 中使用最终值,因此我将使用 json_encode:

$foo = "'Dis here be a \"string\"";
print "<script type='text/javascript'>var foo = " . json_encode($foo) . ";</script>";

它将正确输出:

<script type='text/javascript'>var foo = "'Dis here be a \"string\"";</script>

注意,我没有在 json_encode 函数周围添加一组额外的引号。它会自动添加必要的引号以使其成为有效的 JavaScript 字符串。

Since you are using the final value in JavaScript, I would use json_encode:

$foo = "'Dis here be a \"string\"";
print "<script type='text/javascript'>var foo = " . json_encode($foo) . ";</script>";

And it will correctly output:

<script type='text/javascript'>var foo = "'Dis here be a \"string\"";</script>

Notice I didn't put an extra set of quotes around the json_encode function. It will add the necessary quotes to make it a valid JavaScript string automatically.

走野 2024-08-18 16:14:43

Frank Farmer 的答案很有趣,但它转义了一些不需要转义的东西,比如选项卡。

试试这个片段,它工作得很好:

<script type="text/javascript">
    alert("Hi!\n\tHi!\n<?php echo '\tHI!',"\\n\tHI!";?>");
</script>

由于我总是在 PHP 脚本中连接到数据库,将文本直接传递到 Javascript 字符串中,所以我依靠 real_escape_string 来完成我的肮脏工作。 addslashes() 不处理换行符,这些换行符有时会潜入我传递给 Javascript 的字符串中。

一个简单的 $sql->real_escape_string($string) 就可以让一切变得更好,将数据库输出的任何内容转义为 JavaScript 友好的形式。

Frank Farmer's answer is interesting, but it's escaping some things that don't need to be escaped, like tabs.

Try this snippet, it works just fine:

<script type="text/javascript">
    alert("Hi!\n\tHi!\n<?php echo '\tHI!',"\\n\tHI!";?>");
</script>

Since I'm always connected to a database in my PHP scripts that pass text directly into Javascript strings, I lean on real_escape_string to do my dirty work. addslashes() doesn't handle newlines and those sometimes sneak into strings I'm passing on to Javascript.

A simple $sql->real_escape_string($string) makes it all better, escaping whatever the database spits out into a Javascript-friendly form.

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