How to parse URLs with Chinese characters in PHP?

怪我咯
Release: 2023-03-13 16:16:02
Original
2687 people have browsed it

URL (Uniform Resoure Locator: Uniform Resource Locator) is the address of the WWW page. It is grouped from left to right by the following parts:
· Internet resource type (scheme): Point out Tools used by WWW client programs to operate. For example, "http://" represents the WWW server, "ftp://" represents the FTP server, "gopher://" represents the Gopher server, and "new:" represents the Newgroup news group.
·Server address (host): Indicates the server domain name where the WWW page is located.
·Port: Sometimes (not always), for access to certain resources, the corresponding server port number needs to be given.
·Path: Indicates the location of a resource on the server (its format is the same as that in the DOS system, usually consisting of a directory/subdirectory/file name structure). As with ports, paths are not always required.
The URL address format is arranged as: scheme://host:port/path. For example, http://www.sohu.com/domain/HXWZ is a typical URL address.

This article mainly introduces the sharing of url functions with Chinese characters in PHP. This article directly gives the implementation code, focusing on the use of the rawurlencode function. Friends who need it can refer to it

Many times, when writing web applications, you will encounter problems with conflicts between Chinese and other characters. For example, some URL links contain Chinese characters, so when using wget/curl/file_get_contents, etc. When you wait to get the information, you will directly hit a big "404", which will make you speechless.

A small function is written here to solve this problem. It is only limited to parsing in the path. You can check the manual for the related functions involved.

The code is as follows:

function urlConvert($url){   
        $pathArr = array();   
        $modules = parse_url($url);   
        $path = $modules['path'];   
        $pathSplit = explode(‘/', $path);   
       
        foreach ($pathSplit as $row){   
            $pathArr[] = rawurlencode($row);   
        }   
        $urlNew = $modules['scheme']."://".$modules['host'].implode(‘/', $pathArr);   
        return $urlNew;   
    }
Copy after login

The above is the detailed content of How to parse URLs with Chinese characters in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!