You can use a URL to capture the TITLE of a web page. if some websites cannot find the TITLE, 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. This post was last edited at 11:25:29, January 11 ,.

Curl capture title

The code is written in this way. I don't know if 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 () {// Get in URL $ url = $ _ POST ['URL']; // $ url = "www.pcauto.com.cn"; cannot catch! // 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 ); // Get 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); // captureIf (preg_match ("/(.*?) <\/Title>/I ", $ content_source, $ title) {echo $ title [1];} else {echo 'title pulling failed ';}} </pre> </p>
Reply to discussion (solution)

([\ S \ S] *?) <\/Title> </p>

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 pattern match all characters, including line breaks. If this parameter is not set, line breaks are not included.

Regular modifier

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 pattern match all characters, including line breaks. If this parameter is not set, line breaks are not included.
Thank you very much.

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: info-contact@alibabacloud.com 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.