English 中文(简体)
urlencode only the summary of a URL
原标题:urlencode only the directory and file names of a URL

我需要使用PHP,将URL列入密码。

因此,我想在以下几条编码上打字:http://example.com/file name,并由此形成http://example.com/file%20name

Of course, if I do urlencode( http://example.com/file name ); then I end up with http%3A%2F%2Fexample.com%2Ffile+name.

The obvious (to me, anyway) solution is to use parse_url() to split the URL into scheme, host, etc. and then just urlencode() the parts that need it like the path. Then, I would reassemble the URL using http_build_url().

是否有比这更可取的解决办法? 或者这基本上是走路吗?

最佳回答

@deceze definitely got me going down the right path, so go upvote his answer. But here is exactly what worked:

    $encoded_url = preg_replace_callback( #://([^/]+)/([^?]+)# , function ($match) {
                return  ://  . $match[1] .  /  . join( / , array_map( rawurlencode , explode( / , $match[2])));
            }, $unencoded_url);

值得注意的是:

  • http://www.build_url 如果你向他人分发你的法典(我就是这种情况),那么需要安装一个PECL装置,你可能想避免,并像我在这里那样用reg(从@decezes的回答——aagain中大打一字)。

  • urlencode() is not the way to go! You need rawurlencode() for the path so that spaces get encoded as %20 and not +. Encoding spaces as + is fine for query strings, but not so hot for paths.

  • This won t work for URLs that need a username/password encoded. For my use case, I don t think I care about those, so I m not worried. But if your use case is different in that regard, you ll need to take care of that.

问题回答

正如你所说的那样,应当采取这样的行动:

$parts = parse_url($url);
if (!empty($parts[ path ])) {
    $parts[ path ] = join( / , array_map( rawurlencode , explode( / , $parts[ path ])));
}
$url = http_build_url($parts);

Or possibly:

$url = preg_replace_callback( #https?://.+/([^?]+)# , function ($match) {
           return join( / , array_map( rawurlencode , explode( / , $match[1])));
       }, $url);

(尚未全面测试)

function encode_uri($url){
    $exp = "{[^0-9a-z_.!~* ();,/?:@&=+$#%[]-]}i";
    return preg_replace_callback($exp, function($m){
        return sprintf( %%%02X ,ord($m[0]));
    }, $url);
}

Much simpler:

$encoded = implode("/", array_map("rawurlencode", explode("/", $path)));

我认为这一职能是:

function newUrlEncode ($url) {
    return str_replace(array( %3A ,  %2F ),  / , urlencode($url));
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签