You can use a URL to capture the TITLE of a web page. if some websites cannot find the TITLE, the method is stupid. At the end of this post, the code edited by u016911 from 2013-11-0411: 25: 29 was written by myself. I don't know if there are any better methods. Please give me some tips. some websites can be caught, such as Baidu and some websites. for example, Pacific automobile crawls the TITLE of a webpage through a URL, and some websites cannot be caught. the method is stupid, please advise.
At the end of this post, the code was edited by u0201716911 at 11:25:29 on. I did not know whether there are any better methods. Please give me some advice
Some websites can be caught, such as Baidu and some websites, such as the homepage of Pacific automobile.
Public function set_title ()
{
// Obtain the URL
$ Url = $ _ POST ['URL'];
// $ Url = "www.pcauto.com.cn"; no!
// A series of curl settings
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_ENCODING, 'gzip ');
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
$ Content_source = curl_exec ($ ch );
Curl_close ($ ch );
// Obtain the encoding format of captured content
$ Encode = mb_detect_encoding ($ content_source, array ('gb2312', 'gbk', 'utf-8', 'ascii '));
// Transcoding
$ Content_source = iconv ($ encode, 'utf-8 // IGNORE ', $ content_source );
// Capture
If (preg_match ("/(.*?) <\/Title>/I ", $ content_source, $ title ))
{
Echo $ title [1];
}
Else
{
Echo 'failed to pull the title ';
}
}
Share the title captured by curl:
------ Solution --------------------
The problem lies in Regular Expression Matching. you just need to add an s modifier.
If (preg_match ("/ (.*?) <\/Title>/is ", $ content_source, $ title ))
S if this modifier is set, the DOT metacharacters (.) in the mode (.)
Match all characters, including line breaks. If this parameter is not set, line breaks are not included..