Home > Backend Development > PHP Tutorial > How to parse Chinese characters in url in php

How to parse Chinese characters in url in php

*文
Release: 2023-03-18 11:48:02
Original
2360 people have browsed it

Sometimes you may encounter URLs with Chinese characters. This article mainly introduces how PHP parses Chinese characters in URLs. It uses the rawurlencode function and uses example code to let everyone understand how PHP parses Chinese characters in URLs.

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 you use wget/curl/file_get_contents, etc. to obtain information, You will hit a big "404" directly, and you will be 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.

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

Related recommendations:

Use PATH_INFO to achieve search engine friendly hiding index.php_PHP tutorial

Php's Http request urlencode/rawurlencode

PHP In-depth analysis of rawurlencode and urlencode functions_PHP tutorial

The above is the detailed content of How to parse Chinese characters in url 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