我想转义三个字符,这些是
- 单引号 (')
- 双引号 (")
- 反斜杠 ()
我的href值是
test.html?key="test' me'"&event=3
我想像我们一样php
通过调用来修复它addslashes function
<a href="test.html?key="test' me'"&event=3">test</a>
注意:我需要一种动态的方式
我想转义三个字符,这些是
我的href值是
test.html?key="test' me'"&event=3
我想像我们一样php
通过调用来修复它addslashes function
<a href="test.html?key="test' me'"&event=3">test</a>
注意:我需要一种动态的方式
获取数据并生成正确编码的查询字符串的 PHP 函数是http_build_query
. 然后,您可以将其放入 URL 中,然后对其进行编码htmlspecialchars
以将其插入文档中。
<?php
$base = "test.html";
$query_data = Array(
"key" => "\"test' me'\"",
"event" => 3
);
$url = $base . "?" . http_build_query($query_data);
$html_safe_url = htmlspecialchars($url);
?>
<a href="<?= $html_safe_url ?>">test</a>
您必须对特殊字符进行 URL 编码。
A " 将变为 %22
A ' 将变为 %27
A \ 将变为 %5C
你的锚必须是
test.html?key=%22test%27%20me%27%22&event=3
有关 url 编码的更多信息,请访问http://www.w3schools.com/tags/ref_urlencode.asp。
编码 URL 可能
<a href="test.html?key=%22test'%20me'%22&event=3">test</a>
在w3schools查看这些 url 编码
添加:
var $key ="\"test'\"";
var $event = "3";
print '<a href="test.html?key=' . urlencode($key) .'&event=' . urlencode($event) .'">test</a>';
我不完全知道php字符串如何转义像“但我想\”这样的特殊字符。