且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在锚标记的href中转义字符

更新时间:2022-11-03 09:55:48

用于获取数据并生成正确编码的查询字符串的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>