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!