PHP collection to obtain the content of the specified URL _ PHP Tutorial

Source: Internet
Author: User
PHP collects the content of a specified URL. By referring to others' ideas and turning them into your own ideas, you will find that you will have the ideas and methods to temporarily solve many problems. Copy the code as follows :? Php * function: get a page to refer to others' ideas and turn them into your own ideas. you will find that you will have a temporary solution to many problems.

The code is as follows:


/*
Function: get the page content and store it for reading. lost63
*/
Class GetUrl {
Var $ url; // address
Var $ result; // result
Var $ content; // content
Var $ list; // list
Function GetUrl ($ url ){
$ This-> url = $ url;
$ This-> GetContent ();
$ This-> GetList ();
$ This-> FileSave ();
// Print_r ($ this-> list [2]);
}
Private function GetContent (){
$ This-> result = fopen ($ this-> url, "r ");
While (! Feof ($ this-> result )){
$ This-> content. = fgets ($ this-> result, 9999 );
}
}
Private function GetList (){
Preg_match_all ('/(.*?) <\/A>/', $ this-> content, $ this-> list );
$ This-> list [2] = array_unique ($ this-> list [2]); // remove the same value
While (list ($ key, $ value) = each ($ this-> list [2]) {
If (strpos ($ value, ". html") = 0 | strpos ($ value, "jiaocheng") = 0 ){
Unset ($ this-> list [2] [$ key]);
} Else {
$ This-> list [2] [$ key] = substr ($ value, 0, strpos ($ value ,". html ")). ". html "; // remove unnecessary tags
}
}
}
Private function FileSave (){
Foreach ($ this-> list [2] as $ value ){
$ This-> url = $ value; // assign a value again
$ This-> content = null;
$ This-> GetContent (); // Extract content
Preg_match_all ('/(.*?) <\/Title>/', $ this-> content, $ files); // obtain the title
$ Filename = $ files [1] [0]. ". html"; // storage name
$ Content = $ this-> str_cut ($ this-> content, 'http: // pagead2.googlesyndication.com/pagead/show_ads.js ','

');
$ File = fopen ($ filename, "w ");
Fwrite ($ file, $ content );
Fclose ($ file );
Echo $ filename. "save OK
\ N ";
}
}
Function str_cut ($ str, $ start, $ end ){
$ Content = strstr ($ str, $ start );
$ Content = substr ($ content, strlen ($ start), strpos ($ content, $ end)-strlen ($ start ));
Return $ content;
}
}
$ W = new GetUrl ("http://www.ijavascript.cn/jiaocheng/javascript-jiaocheng-352.html ");
?>

Bytes. The code is as follows :? Php/* function: get the page...

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.