Some websites cannot capture the TITLE of a webpage through a URL. the method is stupid.

Source: Internet
Author: User
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 = ""; 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];
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..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.