【发布时间】:2019-09-25 03:55:12
【问题描述】:
我有这个代码:
<html>
<?php
$var="\'quotes\" àccènts"; // 'quotes" àccènts
?>
<button onclick="fun('<?php echo $var ?>');">click me</button>
</html>
但是当我点击时,我在控制台中得到“Uncaught SyntaxError: Invalid or unexpected token”
我试过了:
<button onclick="fun('<?php echo $var ?>');">click me</button>
<?php
function escape($var)
{
$var=str_replace("'","\\'",$var);
$var=str_replace("\"",""",$var);
$var=trim(preg_replace("/\s+/"," ",$var));
return $var
}
?>
但是后来重音变成了未知字符,我该如何在 inline html 中解决这个问题?
【问题讨论】:
-
使用ajax返回你想要的值——混合JS和PHP总是一个坏主意
-
请解释为什么你需要内联 html 才能工作,如果不破坏原始字符串,就不可能在 onclick 属性中内联它,但如果你充分解释你的用例和需要,也许可以找到一种解决方法对于内联html
标签: javascript php escaping quotes