如何在锚标记的href中转义字符 [英] How do I escape characters in href of anchor tag

查看:172
本文介绍了如何在锚标记的href中转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转义三个字符

  1. 单引号(')
  2. 双引号()
  3. 反斜杠()

我的href值为 test.html?key="test' me'"&event=3

我想像在php中一样通过调用addslashes function

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>

注意:我需要一种动态的方式

推荐答案

用于获取数据并生成正确编码的查询字符串的PHP函数是http_build_query.然后,您可以将其放在URL中,然后使用htmlspecialchars对其进行编码,以将其插入文档中.

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中转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆