PHP obtains the remote website title and description

Source: Internet
Author: User
PHP obtains the remote website title and description. This post was last edited by mingniu66 from 2013-11-6:3232:18 & lt ;? Php & nbsp; header (Content-Type: texthtml; charsetutf-8); $ url & nbsp; cm100.net; & nbsp; PHP get remote website title and description

This post was last edited by mingniu66 at 16:32:18, January 20 ,.

Header ("Content-Type: text/html; charset = utf-8 ");
$ Url = 'http: // cm100.net /';

// Obtain the code of the remote webpage
Function _ charset ($ url ){
$ Text = file_get_contents ($ url );
$ Mode = '/charset = (. *) \ "/iU ';
Preg_match ($ mode, $ text, $ result );
Return $ result [1];
}
$ Charset = _ charset ($ url); // get the code of the remote webpage
// Public


// 1: get the title of the remote webpage, $ url address, $ charset is used to determine the encoding
Function _ title ($ url, $ charset ){
$ Text = file_get_contents ($ url );

// Garbled characters are processed for gb2312 encoding.
If ($ charset = 'gb2312 '){
$ Text = iconv ('gb2312', 'utf-8', $ text );
}

$ Mode = '/ (. *) <\/Title>/iU ';
Preg_match ($ mode, $ text, $ result );
Return $ result [1];
}
Echo 'website title: '. $ title = _ title ($ url, $ charset); // Obtain the Remote Webpage title



// 2: Get the remote webpage introduction
Function _ description ($ url, $ charset ){
$ Text = file_get_contents ($ url );

// Garbled code processing for gb2312
If ($ charset = 'gb2312 '){
$ Text = iconv ('gb2312', 'utf-8', $ text );
}

$ Mode = '/ /IU ';
Preg_match ($ mode, $ text, $ result );
Return $ result [1];

}
Echo 'website introduction: '. $ description = _ description ($ url, $ charset); // Obtain the remote webpage introduction
?>

PHP beginners want to obtain the title and description of remote websites. after checking the results written on the Internet for several days, they can barely achieve the desired results. The retrieval speed is a little slow and the code is messy, I wonder if there is any better way,

Share:


------ Solution --------------------
You can synthesize a function, so it should be very fast.
------ Solution --------------------
How many times does a page need to be retrieved?
------ Solution --------------------
Http://blog.csdn.net/jdgdf566/article/details/13632111
------ Solution --------------------
You can see that file_get_contents is used in each function. Of course it's slow.
There should be only one function for file_get_contents to get the content, and then pass the content to each function to match the result!

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.