Collection functions commonly used by PHP collection programs

Source: Internet
Author: User
Collection functions commonly used by PHP collection programs. On the php collection page, the most common one is to filter some special characters or collect and save the images in the content, next I will introduce you to some of the common functions when writing a php collection program. The most common function on the php collection page is to filter some special characters or collect and save the images in the content, next I will introduce some common functions when I write a php collection program.

After paying attention to the PHP collection program over the past few days, we found that it is so convenient to collect content using PHP. here we will summarize the frequently used collection functions for future use!












Convert each row and column of an HTML table into an array to collect table data.
Function getTdArray ($ table ){
$ Table = preg_replace ("' ] *?> 'Si "," ", $ table );
$ Table = preg_replace ("' ] *?> 'Si "," ", $ table );
$ Table = preg_replace ("' ] *?> 'Si "," ", $ table );
$ Table = str_replace (""," {Tr} ", $ table );
$ Table = str_replace (""," {Td} ", $ table );
// Remove the HTML tag
$ Table = preg_replace ("'<[/!] *? [^ <>] *?> 'Si "," ", $ table );
// Remove the white space characters
$ Table = preg_replace ("'([rn]) [s] +'", "", $ table );
$ Table = str_replace ("", "", $ table );
$ Table = str_replace ("", "", $ table );
$ Table = explode ('{tr}', $ table );
Array_pop ($ table );
Foreach ($ table as $ key => $ tr ){
$ Td = explode ('{td}', $ tr );
Array_pop ($ td );
$ Td_array [] = $ td;
}
Return $ td_array;
}

Returns all words in the string $ distinct = true to remove duplicates.
Function splitEnStr ($ str, $ distinct = true ){
Preg_match_all ('/([a-zA-Z] +)/', $ str, $ match );
If ($ distinct = true ){
$ Match [1] = array_unique ($ match [1]);
}
Sort ($ match [1]);
Return $ match [1];
}

", '",', $ Table );$ Table = str_replace (" "," {Tr} ", $ table );// Remove the HTML tag$ Table = preg_replace ("'<[/!] *? [^ <>] *?> 'Si "," ", $ table );// Remove the white space characters$ Table = preg_replace ("'([rn]) [s] +'", "", $ table );$ Table = str_replace ("", "", $ table );$ Table = str_replace ("", "", $ table );$ Table = explode (", {tr}", $ table );Array_pop ($ table );Return $ table;}
The code is as follows:

Get all link content and addresses
Function getAllURL ($ code ){
Preg_match_all ('/"'] +) [" | ']? S * [^>] *> ([^>] +)/I ', $ code, $ arr );
Return array ('name' => $ arr [2], 'URL' => $ arr [1]);
}

Get all image addresses
Function getImgSrc ($ code ){
$ Reg = "/] * src =" (http ://(. + )/(. + ). (jpg | gif | bmp | bnp | png) "/isU ";
Preg_match_all ($ reg, $ code, $ img_array, PREG_PATTERN_ORDER );
Return $ img_array [1];
}

Current script URL
Function getSelfURL (){
If (! Empty ($ _ SERVER ["REQUEST_URI"]) {
$ ScriptName = $ _ SERVER ["REQUEST_URI"];
$ Nowurl = $ scriptName;
} Else {
$ ScriptName = $ _ SERVER ["PHP_SELF"];
If (empty ($ _ SERVER ["QUERY_STRING"]) $ nowurl = $ scriptName;
Else $ nowurl = $ scriptName ."? ". $ _ SERVER [" QUERY_STRING "];
}
Return $ nowurl;
}

Convert full-angle numbers into half-angle numbers
Function getAlabNum ($ fnum ){
$ Nums = array ("0", "1", "2", "3", "4", "5", "6", "7 ", "8", "9 ");
$ Fnums = "0123456789 ";
For ($ I = 0; $ I <= 9; $ I ++) $ fnum = str_replace ($ nums [$ I], $ fnums [$ I], $ fnum );
$ Fnum = ereg_replace ("[^ 0-9.] | ^ 0 {1,}", "", $ fnum );
If ($ fnum = "") $ fnum = 0;
Return $ fnum;
}

Remove HTML tags
Function text2Html ($ txt ){
$ Txt = str_replace ("", "", $ txt );
$ Txt = str_replace ("<", "<", $ txt );
$ Txt = str_replace (">", ">", $ txt );
$ Txt = preg_replace ("/[rn] {1,}/isU ","
Rn ", $ txt );
Return $ txt;
}

Clear HTML tags
Function clearHtml ($ str ){
$ Str = str_replace ('<', '<', $ str );
$ Str = str_replace ('>', '>', $ str );
Return $ str;
}

Convert relative paths to absolute paths
Function relative2Absolute ($ content, $ feed_url ){
Preg_match ('/(http | https | ftp): //', $ feed_url, $ protocol );
$ Server_url = preg_replace ("/(http | https | ftp | news): //", "", $ feed_url );
$ Server_url = preg_replace ("//. */", "", $ server_url );
If ($ server_url = ''){
Return $ content;
}
If (isset ($ protocol [0]) {
$ New_content = preg_replace ('/href = "//', 'href =" '. $ protocol [0]. $ server_url.'/', $ content );
$ New_content = preg_replace ('/src = "//', 'src =" '. $ protocol [0]. $ server_url.'/', $ new_content );
} Else {
$ New_content = $ content;
}
Return $ new_content;
}

Obtains the content in the specified tag.
Function getTagData ($ str, $ start, $ end ){
If ($ start = ''| $ end = ''){
Return;
}
$ Str = explode ($ start, $ str );
$ Str = explode ($ end, $ str [1]);
Return $ str [0];
}

Convert each line of an HTML table to an array in CSV format
Function getTrArray ($ table ){
$ Table = preg_replace ("' ] *?> 'Si ", '"', $ table );
$ Table = str_replace ("

...

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.