PHP parse_url utf-8 compatible function mb_parse_url

Article Directory

Introduction

When using parse_url to parse the following URLs, garbled characters will appear (this kind of URL is normally allowed and should be named in the backend)

$url = 'http://www.baidu.com/a/b/c/New Microsoft Word document.docx';
print_r(parse_url($url));

result:

mb_parse_url

in(http://php.net/manual/zh/function.parse-url.phpThere is a related UTF-8 compatible method in ), but it feels that it is not fully compatible with parse_url.

if (!function_exists('mb_parse_url')) {
    /**
     * UTF-8 aware parse_url() replacement.
     *
     * @param $url
     * @param int $component
     * @return mixed|string
     */
    function mb_parse_url($url, $component = -1)
    {
        $encodedUrl = preg_replace_callback(
            '%[^:/?#&=\.]+%usD',
            function ($matches) {
                return urlencode($matches[0]);
            },
            $url
        );

        $components = parse_url($encodedUrl, $component);

        if (is_array($components)) {
            foreach ($components as &$part) {
                if (is_string($part)) {
                    $part = urldecode($part);
                }
            }
        } else if (is_string($components)) {
            $components = urldecode($components);
        }

        return $components;
    }
}

result:

And can pass in the relevant parameters in parse_urlcomponent, Specify one of PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT to get the string of the specified part of the URL. (Except after being specified as PHP_URL_PORT, an integer value will be returned).

==================================
Due to my limited level, if there are any improper expressions and codes in the article, criticisms and corrections are welcome.

Intelligent Recommendation

PHP and UTF-8

No line of solutions. Careful attention to detail, and consistency. UTF-8 sucks in PHP. Forgive my words. PHP is currently at a low level does not support Unicode. There are several ways to ensure the...

Iconv function in PHP to convert encoding, such as UTF-8 to GB2312

Recently, when I was working on the interface of China Construction Bank, I found that the encoding UTF-8 must be converted to GB2312, so I used this iconv function to convert the encoding. It is foun...

Hive - Parse_URL function

Hive's Parse_URL function parse_url Function: Analyze the URL string, Syntax: Parse_url (URL, ParttoExTract [, Key]) PartToExtract options contain [Host, Path, Query, Ref, Protocol, File, Authority, U...

Hive: PARSE_URL, PARSE_URL_TUPLE function

Table of contents Foreword 1. PARSE_URL () 2. Parse_url_tuple () Foreword PARSE_URL, PARSE_URL_TUPLE is the built -in function of the HIVE, which can resolve the URL. The difference is that the PARSE_...

String, encoding, UTF-8 in PHP

Recently I have read a lot of articles on coding, so I have two blog posts about "PHP, String, Coding, UTF-8". This blog post is in the upper part and is divided into four sections, namely &...

More Recommendation

Best practices for PHP and UTF-8

"String, Encoding, UTF-8 in PHP"The article describes some of the basics of the column, it is boring, and now it is useful - the best practice of PHP string processing, this article is the s...

PHP with the Unicode UTF-8 converted to

Reproduced in: https: //blog.51cto.com/happyliu/1551003...

UTF-8 at the PHP and Database level

UTF-8 at PHP Level: Must use the mb_* functions (such as mb_strpos() and mb_strlen() ) whenever operate on Unicode string. For example you use substr() on a UTF-8 string, there's a good chance the res...

In turn php unicode utf-8

Reference article:http://randomchaos.com/documents/?source=php_and_unicode Reproduced in: https: //my.oschina.net/artwl/blog/694297...

PHP utf-8 character set

Increase in php code line: header ( 'Content-type: text / html; charset = utf-8'); such as:  ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top