PHP get remote Web site title and description

Source: Internet
Author: User
This post was last edited by mingniu66 on 2013-11-20 16:32:18

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

Get the encoding for a remote Web page
function _charset ($url) {
$text = file_get_contents ($url);
$mode = '/charset= (. *) \ '/iu ';
Preg_match ($mode, $text, $result);
return $result [1];
}
$charset = _charset ($url);//Get the encoding of the Remote Web page
The above is a public


1: Gets the title of the Remote Web page, $url address, $charset used to determine the encoding
function _title ($url, $charset) {
$text = file_get_contents ($url);

If it is gb2312 encoding processing garbled
if ($charset = = ' gb2312 ') {
$text = Iconv (' gb2312 ', ' utf-8 ', $text);
}

$mode = '/ <title>(. *) <\/title>/iu ';</title>
Preg_match ($mode, $text, $result);
return $result [1];
}
Echo ' website title: '. $title = _title ($url, $charset); Get the title of a remote Web page



2: Get an Introduction to remote Web pages
function _description ($url, $charset) {
$text = file_get_contents ($url);

GB2312 encoding garbled processing
if ($charset = = ' gb2312 ') {
$text = Iconv (' gb2312 ', ' utf-8 ', $text);
}

$mode = '/ /iu ';
Preg_match ($mode, $text, $result);
return $result [1];

}
Echo ' website profile: '. $description = _description ($url, $charset); Get an introduction to remote Web pages
?>

Beginners PHP, want to get the title of the remote site and introduction, the online search for a few days to write out the results, barely able to achieve the desired results, get a bit slow, feel the code is messy, do not know there is no better way,


Reply to discussion (solution)

You can synthesize a function, that should be quick.

How many times do you want to get an analysis page?

http://blog.csdn.net/jdgdf566/article/details/13632111

You see, you use file_get_contents in every function. Of course it's slow.
There should be only 1 functions to file_get_contents the content, and then pass the content to each function to match the result!

Thank you for your answer just 4 people 40 points answer all the same indeed file_get_contents a number of times because the beginner may be laborious to change.

  • 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.