[PHP] Utility function 3_php Instance

Source: Internet
Author: User
Tags explode html tags
Get the current script URL
Copy Code code as follows:

function Get_php_url () {
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 digits to half-width digits
Copy Code code as follows:

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
Copy Code code as follows:

function text2html ($txt) {
$txt = Str_replace ("", "", $txt);
$txt = Str_replace ("<", "<", $txt);
$txt = Str_replace (">", ">", $txt);
$txt = Preg_replace ("/[\r\n]{1,}/isu", "
\ r \ n ", $txt);
return $txt;
}

Clear HTML Tags
Copy Code code as follows:


function clearhtml ($STR) {
$str = Str_replace (' < ', ' < ', $str);
$str = Str_replace (' > ', ' > ', $str);
return $str;
}

Convert a relative path into an absolute path
Copy Code code as follows:


function Relative_to_absolute ($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;
}

Get all Links
Copy Code code as follows:

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

Convert each line of HTML table to CSV format array
Copy Code code as follows:

function Get_tr_array ($table) {
$table = Preg_replace ("' <td[^>]*?> ' si", ' "', $table);
$table = Str_replace ("</td>", ' ", ', $table);
$table = Str_replace ("</tr>", "{tr}", $table);
function Get_tr_array ($table) {
$table = Preg_replace ("' <td[^>]*?> ' si", ' "', $table);
$table = Str_replace ("</td>", ' ", ', $table);
$table = Str_replace ("</tr>", "{tr}", $table);
Get rid of HTML tags
$table = Preg_replace ("' <[\/\!] *? [^<>]*?> ' Si ', "", $table);
Remove white space characters
$table = Preg_replace ("' ([\ r \ n]) [\s]+ '", "", $table);
$table = Str_replace ("", "", $table);
$table = Str_replace ("", "", $table);

$table = Explode (", {tr}", $table);
Array_pop ($table);
return $table;
}

To convert each row of HTML tables into an array, collect tabular data
Copy Code code as follows:

function Get_td_array ($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);
Get rid of HTML tags
$table = Preg_replace ("' <[\/\!] *? [^<>]*?> ' Si ', "", $table);
Remove white space characters
$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 words in a string $distinct =true remove Duplicates
Copy Code code as follows:

Function split_en_str ($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]; 
}

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.