import std.conv : toHexString;
import std.string : format;

string urlencode(string str)
{
    const char[] reserved = "-_.!~*'()";
    const char[] hex = "0123456789ABCDEF";
    string result;
    foreach (char c; str)
    {
        if (c.isAlphaNumeric || c in reserved)
            result ~= c;
        else if (c == ' ')
            result ~= '+';
        else
            result ~= format("%%%s", toHexString(c).toUpper());
    }
    return result;
}

wstring urlencode(wstring str)
{
    const wchar[] reserved = "-_.!~*'()";
    const wchar[] hex = "0123456789ABCDEF";
    wstring result;
    foreach (wchar c; str)
    {
        if (c.isAlphaNumeric || c in reserved)
            result ~= c;
        else if (c == ' ')
            result ~= '+';
        else
            result ~= format("%%%s", toHexString(c).toUpper());
    }
    return result;
}
``
请用dlang语言实现php的urlencode函数最好同时支持string和wstring关于该函数的介绍如下:urlencodePHP 4 PHP 5 PHP 7 PHP 8urlencode — 编码 URL 字符串说明 ¶urlencodestring $string string此函数便于将字符串编码并将其用于 URL 的请求部分同时它还便于将变量传递给下一页。参数 ¶string要编码

原文地址: https://www.cveoy.top/t/topic/gS63 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录