我如何逃避锚标签的href中的charachters [英] how do i escape charachters in href of anchor tag
本文介绍了我如何逃避锚标签的href中的charachters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想转义三个字符这些是
- 单引号(')
- 双quote()
- backslash()
我的href值是
test.html?key =test'me'& event = 3
我想将其解决为我们在 php
中通过调用 addslashes函数
<$ p $
< a href =test.html?key =test'me'& event = 3> test< / a> p>
注意:我需要一种动态的方式来做到这一点 >解决方案
获取数据并生成正确编码的查询字符串的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?>>测试< / a>
I want to escape three characters these are
- single quote (')
- double quote (")
- backslash ()
my href value is
test.html?key="test' me'"&event=3
I want to fix it as we do in php
by calling addslashes function
<a href="test.html?key="test' me'"&event=3">test</a>
NOTE: I need a dynamic way of doing it
解决方案
The PHP function to take data and generate a properly encoded query string is http_build_query
. You can then put it in a URL and then encode that using htmlspecialchars
to insert it in a document.
<?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>
这篇关于我如何逃避锚标签的href中的charachters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文