One data collection class _ PHP Tutorial

Source: Internet
Author: User
A data collection class. Copy the code as follows :? Compatible with php4php5 program author Zhang Jian 52linux.com (I love Linux) contact method 733905@qq.comQQ733905 simple call method *? Include (ugs. php); you can The code is as follows:


// Compatible with php4 php5
// Program author Zhang jian52linux.com (I love Linux)
// Contact 733905@qq.com QQ 733905
// Simple call method
/*
Include ("ugs. php"); // You can download this ugs. phps and rename it ugs. php
$ Ugs = new ugs ();
$ Url = "http://domainname.com/path_to_your_target? Param ";
$ Ugs-> seturl ($ url );
$ Ugs-> gather ();
// ...... Other methods in this class can be called here to adjust $ ugs-> value,
To meet your requirements
$ Content = $ ugs-> getcontent ();
Print ($ content );
?>
*/
Class ugs
{
Var $ value _; // 'target content
Var $ src _; // 'Destination URL address

Function seturl ($ url)
{
$ This-> src _ = $ url;
}
Function getcontent ()
{
Return $ this-> value _;
}
Function getfile ($ url)
// Obtain the target
{
$ Url_parsed = parse_url ($ url );
$ Host = $ url_parsed ["host"];
$ Port = $ url_parsed ["port"];
If ($ port = 0) $ port = 80;
$ Path = $ url_parsed ["path"];
If (empty ($ path ))
$ Path = "/";
If ($ url_parsed ["query"]! = "")
$ Path. = "? ". $ Url_parsed [" query "];
$ Out = "GET $ path HTTP/1.0 \ r \ nHost: $ host \ r \ n ";
$ Fp = fsockopen ($ host, $ port, $ errno, $ errstr, 30 );
Fwrite ($ fp, $ out );
$ Body = false;
While (! Feof ($ fp ))
{
$ S = fgets ($ fp, 1024 );
If ($ body) $ in. = $ s;
If ($ s = "\ r \ n ")
$ Body = true;
}
Fclose ($ fp );
Return $ in;
}

Function getfile_curl ($ url)
{
$ Curl = "/usr/local/bin/curl"; // path to your curl
$ Curl_options = "-s -- connect-timeout 10 -- max-time 10 ";
// For curl usage, see curl -- help or man curl.
// Curl parameters are very rich. you can simulate various browsers (agents) and set referer
$ Cmd = "$ curl $ curl_options $ url ";
@ Exec ($ cmd, $ o, $ r );
If ($ r! = 0)
{
Return "timeout ";
}
Else
{
$ O = join ("", $ o );
Return $ o;
}
}

Function gather_curl ($ curl)
{
$ Http = $ this-> getfile_curl ($ this-> src _);
Return $ this-> value _ = $ http;
}

Function gather_array ($ url)
{
Return file ($ url );
}

Function gather ()
// Start Collection
{
$ Http = $ this-> getfile ($ this-> src _);
Return $ this-> value _ = $ http;
}

Function gather_local ($ toline = true)
// Process Local files
{

If ($ toline)
{
$ Http = file ($ this-> src _);
Return $ this-> value _ = $ this-> BytesToBstr ($ http );
}
Else
{
$ Http = file ($ this-> src _);
Return $ this-> value _ = $ http;
}



}

Function noReturn ()
// Delete the carriage return and line feed
{
$ This-> value _ = str_replace ("\ n", "", $ this-> value _);
$ This-> value _ = str_replace ("\ r", "", $ this-> value _);
}


Function change ($ oldStr, $ str)
// 'Replace the individual strings in the collected content with the new value/method
// The 'parameters are old strings and new strings respectively
{
$ This-> value _ = str_replace ($ oldStr, $ str, $ this-> value _);
}



Function cut ($ start, $ end, $ no = '1', $ comprise = '')
// 'Cut the collected content by specifying the first and end strings (excluding the first and end strings)
// $ No must be 1, 2, 3... 0 is not allowed
// $ Comprise can select start, end, all, or nothing
{
$ String = explode ($ start, $ this-> value _);
// Print_r ($ string );
$ String = explode ($ end, $ string [$ no]);
// Print_r ($ string );
Switch ($ comprise ){
Case 'start ':
$ String = $ start. $ string [0];
Break;
Case 'end ':
$ String = $ string [0]. $ end;
Break;
Case 'all ':
$ String = $ start. $ string [0]. $ end;
Break;
Default:
$ String = $ string [0];
}
Return $ this-> value _ = $ string;
}


Function filt ($ head, $ bot, $ str, $ no = '1', $ comprise = '')
// 'Replace the collected content with a new value (excluding the first and last strings) based on the specified string
// The 'parameters are the first string, the last string, and the new value. if the new value is null, the filter is used.
{
$ Tmp_v = $ this-> value _;
$ Tmp = $ this-> cut ($ head, $ bot, $ no, $ comprise );
Return $ this-> value _ = str_replace ($ tmp, $ str, $ tmp_v );
}



Function local ()
{
// 'Change the absolute URL address in the collected content to the local relative address
// Not implemented yet
}



Function replaceByReg ($ patrn, $ str)
// 'Replace the string that matches the regular expression with the new value in the collected content/method
// The 'parameter is your custom regular expression, new value
{
Return $ this-> value _ = join ("", preg_replace ($ patrn, $ str, $ this-> value _));
}



Function debug ()
// Debug display
{
$ Tempstr = "SCRIPT function runEx () {var winEx2 = window. open (\ "\", \ "winEx2 \", \ "width = 500, height = 300, status = yes, menubar = no, scrollbars = yes, resizable = yes \ "); winEx2.document. open (\ "text/html \", \ "replace \"); winEx2.document. write (unescape (event. srcElement. parentElement. children [0]. value); winEx2.document. close ();} function saveFile () {var comment '); win. close ();} SCRIPT

". $ This-> value _."

";
Echo $ tempstr;
}


}
?>

The http://www.bkjia.com/PHPjc/317743.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/317743.htmlTechArticle code is as follows :? // Compatible with php4php5 // program author Zhang jian52linux.com (I love Linux) // contact method 733905@qq.comQQ733905 // simple call method /*? Include ("ugs. php"); // you can...

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.