Get the App
SLTechnology News&Howtos  ›  Development  › 

Url Chinese coding problem when how to use Curl to crawl remote content

Shulou Source: shulou.com Published: 2022-06-03 07:40:06 11月15日 Update

This article introduces the knowledge of "how to use Curl to crawl remote content when url Chinese coding problem". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

For URL coding in PHP, you can use urlencode () or rawurlencode (). The difference is that the former encodes spaces as'+ 'and the latter encodes spaces as'% 20', but it should be noted that only part of the URL should be encoded, otherwise colons and backslashes in URL will also be escaped. The following is a detailed explanation:

The copy code is as follows:

String urlencode (string str)

Returns a string except-_. All non-alphanumeric characters outside are replaced with a percent sign (%) followed by two hexadecimal digits, and spaces are encoded with a plus sign (+).

Example 1: the difference between urlencode function and rawurlencode function

The copy code is as follows:

$str=' blog'

Echo urlencode ($str)

Echo "

"

Echo rawurlencode ($str)

Url results:

The copy code is as follows:

% B2%A9+%BF%CD

% B2%A9%20%BF%CD

Example 2: url Chinese coding method

From url: "http://www.baidu.com/s?wd= blog" to url: "http://www.baidu.com/s?wd=%E5%8D%9A%20%E5%AE%A2";

The copy code is as follows:

$url=' http://www.baidu.com/s?wd= blog'

$arr=explode ('=', $url)

$url=$arr [0].'= '.rawurlencode ($arr [1])

Echo $url

Results:

Http://www.baidu.com/s?wd=%E5%8D%9A%20%E5%AE%A2

Or use the following url encoding function

The copy code is as follows:

Function cn_urlencode ($url) {

$pregstr = "/ [\ x {4e00} -\ x {9fa5}] + / u"; / / UTF-8 Chinese regular

If (preg_match_all ($pregstr,$url,$matchArray)) {/ / matches Chinese and returns an array

Foreach ($matchArray [0] as $key= > $val) {

$url=str_replace ($val, urlencode ($val), $url); / / will be translated into Chinese

}

If (strpos ($url,'')) {/ / if there are spaces

$url=str_replace (',','% 20 minutes minute URL)

}

}

Return $url

}

Url results:

Http://www.baidu.com/s?wd=%E5%8D%9A%20%E5%AE%A2

This is the end of the content of "how to use Curl to crawl remote content in url Chinese coding problem". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Coding code Chinese content spaces functions characters results questions examples strings more knowledge practicality learning and then colons semicolons plus hexadecimal Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Redmi MySQL Shulou Technology vpn