【发布时间】:2010-12-18 03:14:25
【问题描述】:
我在我的 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() 函数的东西...
【问题讨论】:
-
这些“各种原因”有哪些?
-
你打开了魔术引号吗?这可能是问题所在。
-
各种原因是这个js是由smarty模板生成的,smarty不允许你使用完整的php,但是{$foo|addslashes}可以解决问题
标签: php javascript quotes quoting