PHP 中的 encodeURI() ? [英] encodeURI() in PHP?

查看:22
本文介绍了PHP 中的 encodeURI() ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP 中是否有一些不编码的 encodeURI() 函数:~!@#$&*()=:/,;?+'?

Is there some encodeURI() function in PHP that does not encode: ~!@#$&*()=:/,;?+'?

推荐答案

我正在使用这个

function encodeURI($url) {
    // http://php.net/manual/en/function.rawurlencode.php
    // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI
    $unescaped = array(
        '%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!', '%7E'=>'~',
        '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')'
    );
    $reserved = array(
        '%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':',
        '%40'=>'@','%26'=>'&','%3D'=>'=','%2B'=>'+','%24'=>'$'
    );
    $score = array(
        '%23'=>'#'
    );
    return strtr(rawurlencode($url), array_merge($reserved,$unescaped,$score));

}

它基本上对所有内容进行原始编码,然后将一些内容解码回来(正如 Zanlok 在他的评论中所建议的那样).这应该符合 encodeURI 的 Mozilla 规范.

It basically rawurlencodes everything, and then decodes a few things back (as Zanlok suggested in his comment). This should conform to the Mozilla specs of encodeURI.

在 MDN 之后,如果希望遵循更新的 URL RFC3986",请添加

And following MDN, 'if one wishes to follow the more recent RFC3986 for URLs', add

function fixedEncodeURI($url) {
    return strtr(encodeURI($url),array('%5B'=>'[', '%5D'=>']'));
}

这篇关于PHP 中的 encodeURI() ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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