Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report