PHP collection to get the content of a specified URL _ php skills

Source: Internet
Author: User
Php is used to obtain the implementation code of the specified webpage content. generally, some code is often used by the collection program. this is just a simple Foundation. For more detailed information, refer to the php collection finished products and the source code that can be used to learn more. 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.

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 ");
?>

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.