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.