简体   繁体   English

带cURL的PHP​​编码URL

[英]PHP encoding URL with cURL

I am trying to access an array of URLs with cURL. 我正在尝试使用cURL访问URL数组。 Some of the links look like the following: 一些链接如下所示:

 http://somelink.com/directory/file name.php
 http://somelink.com/file name.php
 http://somelink.com/(some file).txt
 http://somelink.com/directory/random chars &^%.txt
 http://somelink.com/!@#$%^&*.txt

Just some files with some random characters. 只是一些带有一些随机字符的文件。 I know there is the urlencode() function, but this function also url encodes the http:// and the / after each directory, which will not allow the page to load properly in cURL. 我知道有urlencode()函数,但是此函数还在每个目录后对URL http://和/进行URL编码,这将不允许页面在cURL中正确加载。 Is there some regular expression that can be used to separate the words after each / and encode them individually, but ignore the encoding of everything before the root directory /? 是否有一些正则表达式可用于分隔每个/之后的单词并分别对其进行编码,但忽略根目录/之前的所有内容的编码?

You can write a function to take input in the format you have (array) and output it in the desired format like this: 您可以编写一个函数以采用您拥有的格式(数组)输入并以所需的格式输出它,如下所示:

function urlencode_array($url_array)
{
    $encoded = array();

    foreach ($url_array as $url) {
        $encoded[] = "http://" . str_replace("%2F", "/", rawurlencode(substr($url, 7)));
    }

    return $encoded;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM