Summary of commonly used collection functions in PHP (with code)

Source: Internet
Author: User
This article brings the content is about PHP commonly used in the collection function summary (with code), there is a certain reference value, the need for friends can refer to, I hope to help you.

These days concerned about the PHP collection procedures, only to find the content with PHP is so convenient to use the collection function is summarized here, convenient for later use!

Get all linked content and addresses

function Getallurl ($code) {preg_match_all ('/<a\s+href=["|\ ']? ( [^> "\ ']+) [" |\ ']?\s*[^>]*> ([^>]+] <\/a>/i ', $code, $arr); return Array (' name ' = = $arr [2], ' url ' = > $arr [1]);}

Get all the picture addresses

function Getimgsrc ($code) {$reg = "/]*src=\" (http:\/\/(. +) \ (. +) \. ( jpg|gif|bmp|bnp|png) \ "/isu";p reg_match_all ($reg, $code, $img _array, Preg_pattern_order); return $img _array[1];}

The 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-width numbers to half-width 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 ("/[\r\n]{1,}/isu "," <br/>\r\n ", $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;}

Gets the contents of the specified tag

function Gettagdata ($str, $start, $end) {if ($start = = ' | | $end = = ') {return;} $str = Explode ($start, $str); $str = Explode ($end, $str [1]); return $str [0];}

Each line of the HTML table is converted to a CSV format array

function Gettrarray ($table) {$table = Preg_replace ("' <td[^>]*?> ' si ', '" ', $table); $table = Str_replace ("</ Td> ", '", ', $table); $table = Str_replace ("</tr>", "{tr}", $table);//Remove HTML Tag $table = preg_replace ("' <[\/\!] *? [^<>]*?> ' si ', ' ", $table);//Remove the whitespace character $table = Preg_replace (" ' ([\ r \ n]) [\s]+ ' "," ", $table); $table = Str_replace (" "," ", $table); $table = Str_replace (" "," "", $table); $table = Explode (", {tr}", $table); Array_pop ($table); return $table;}

Convert each row of an HTML table to an array, collecting tabular data

function Gettdarray ($table) {$table = Preg_replace ("' <table[^>]*?> ' si '," ", $table); $table = Preg_replace (" ' <tr[^>]*?> ' Si ', "", $table); $table = Preg_replace ("' <td[^>]*?> ' si '," ", $table); $table = Str_replace ("</tr>", "{tr}", $table), $table = Str_replace ("</td>", "{td}", $table);//Remove HTML Tag $table = preg_replace ("' <[\/\!] *? [^<>]*?> ' si ', ' ", $table);//Remove the whitespace character $table = Preg_replace (" ' ([\ r \ n]) [\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 the words in a string $distinct =true 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];} 
Related Article

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.