代码之家  ›  专栏  ›  技术社区  ›  Marco Bernardini

显示带有引号的URL的输入字段

  •  1
  • Marco Bernardini  · 技术社区  · 11 年前

    我必须将一些维基百科URL存储到MariaDB数据库中。

    碰巧有些URL包含引号,例如:

    https://en.wikipedia.org/wiki/%22Heroes%22

    所以我用 urlencode() 将它们存储为 "en.wikipedia.org%2Fwiki%2F%22Heroes%22" .

    如果我urldecode()URL,将其显示在 <input type="text"> 字段中没有所有的%(它们会吓到不熟练的用户) input 价值

    我找到了此解决方法,以更舒适的方式显示结果:

    $url = 'en.wikipedia.org%2Fwiki%2F%22Heroes%22'; // it comes in this way from the DB
    $tmp = str_replace('%22','&quot;', $url);
    $url_input = urldecode($tmp);
    echo "<input type=\"text\" value=\"$url_input\" />";
    

    的值 $url_input 作为一个 <a href 锚定,然后使用 FILTER_SANITIZE_URL url编码器() 以将其存储在DB中。

    有更好的方法吗?

    1 回复  |  直到 11 年前
        1
  •  1
  •   Nahid Bin Azhar    11 年前

    只需使用htmlspecialchars()而不是str_replace()

    $url = 'en.wikipedia.org%2Fwiki%2F%22Heroes%22'; // it comes in this way from the DB
    //$tmp = str_replace('%22','&quot;', $url);
    $url_input = htmlspecialchars(urldecode($url));
    echo "<input type=\"text\" value=\"$url_input\" />";
    

    我认为它会比这更好用

    推荐文章