当前位置:首页 > PHP教程 > php高级应用 > 列表

收藏PHP常用自定义函数

发布:smiling 来源: PHP粉丝网  添加日期:2016-08-25 13:22:48 浏览: 评论:0 

本文章为各位收藏PHP常用自定义函数了,这些函数是我们开发应用中常用到了,希望文章对各位会带来帮助.

  1. <?  
  2. function GetIP() { //获取IP  
  3.     if ($_SERVER["HTTP_X_FORWARDED_FOR"])  
  4.         $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; //OSPHP.com.CN  
  5.     else if ($_SERVER["HTTP_CLIENT_IP"])  
  6.         $ip = $_SERVER["HTTP_CLIENT_IP"];  
  7.     else if ($_SERVER["REMOTE_ADDR"])  
  8.         $ip = $_SERVER["REMOTE_ADDR"];  
  9.     else if (getenv("HTTP_X_FORWARDED_FOR"))  
  10.         $ip = getenv("HTTP_X_FORWARDED_FOR");  
  11.     else if (getenv("HTTP_CLIENT_IP"))  
  12.         $ip = getenv("HTTP_CLIENT_IP");  
  13.     else if (getenv("REMOTE_ADDR"))  
  14.         $ip = getenv("REMOTE_ADDR");  
  15.     else  
  16.         $ip = "Unknown";  
  17.     return $ip;  
  18. }  
  19. ?> 
  20.  
  21.  
  22. <?php  
  23. function DateAdd($date, $int, $unit = "d") { //时间的增加(还可以改进成时分秒都可以增加,有时间再补上)  
  24.     $dateArr = explode("-", $date);  
  25.     $value[$unit] = $int; //OSPHP.COm.CN  
  26.     return date("Y-m-d", mktime(0,0,0, $dateArr[1] + $value['m'], $dateArr[2] + $value['d'], $dateArr[0] + $value['y']));  
  27. } 
  28. function GetWeekDay($date) {  //计算出给出的日期是星期几  
  29.     $dateArr = explode("-", $date);  
  30.     return date("w", mktime(0,0,0,$dateArr[1],$dateArr[2],$dateArr[0])); //OsPHP.COM.CN  
  31. }  
  32. ?> 
  33.  
  34.  
  35. <?  
  36. function check_date($date) { //检查日期是否合法日期  
  37.     $dateArr = explode("-", $date);  
  38.     if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) {  
  39.         return checkdate($dateArr[1],$dateArr[2],$dateArr[0]);  
  40.     }  
  41.     return false;  
  42. }  
  43. function check_time($time) {  //检查时间是否合法时间  
  44.     $timeArr = explode(":", $time);  
  45.     if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) { //开源代码OSPhP.COm.CN  
  46.         if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59))  
  47.             return true;  
  48.         else  
  49.             return false;  
  50.     }  
  51.     return false;  
  52. } 
  53. function DateDiff($date1, $date2, $unit = "") { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天  
  54.     switch ($unit) {  
  55.         case 's':  
  56.             $dividend = 1;  
  57.             break;  
  58.         case 'i':  
  59.             $dividend = 60;  
  60.             break;  
  61.         case 'h':  
  62.             $dividend = 3600;  
  63.             break;  
  64.         case 'd':  
  65.             $dividend = 86400;  
  66.             break;  
  67.         default:  
  68.             $dividend = 86400;  
  69.     }  
  70.     $time1 = strtotime($date1);  
  71.     $time2 = strtotime($date2);  
  72.     if ($time1 && $time2)  
  73.         return (float)($time1 - $time2) / $dividend;  
  74.     return false;  
  75. } 
  76. ?> 

PHP重定向

  1. <?  
  2. 方法一:header("Location: index.php");  
  3. 方法二:echo "<scrīpt>window.location ="$PHP_SELF";</scrīpt>"; //PHP开源代码  
  4. 方法三:echo "<META HTTP-EQUIV="Refresh" CONTENT="0; URL=index.php">"; 
  5. ?> 

获取访问者浏览器

  1. <?  
  2. function browse_infor()  
  3. {  
  4. $browser="";$browserver="";  
  5. $Browsers =array("Lynx","MOSAIC","AOL","Opera","JAVA","MacWeb","WebExplorer","OmniWeb");  
  6. $Agent = $GLOBALS["HTTP_USER_AGENT"];  
  7. for ($i=0; $i<=7; $i++)  
  8. {  
  9. if (strpos($Agent,$Browsers[$i]))  
  10. {  
  11. $browser = $Browsers[$i];  
  12. $browserver ="";  
  13. }  
  14. }  
  15. if (ereg("Mozilla",$Agent) && !ereg("MSIE",$Agent))  
  16. {  
  17. $temp =explode("(", $Agent); $Part=$temp[0];  
  18. $temp =explode("/", $Part); $browserver=$temp[1]; //开源代码OSPhP.COm.CN  
  19. $temp =explode(" ",$browserver); $browserver=$temp[0];  
  20. $browserver =preg_replace("/([d.]+)/","1",$browserver);  
  21. $browserver = " $browserver"; //oSPHP.COM.CN  
  22. $browser = "Netscape Navigator";  
  23. }  
  24. if (ereg("Mozilla",$Agent) && ereg("Opera",$Agent))  
  25. {  
  26. $temp =explode("(", $Agent); $Part=$temp[1]; //OSPHP.COM.Cn开源  
  27. $temp =explode(")", $Part); $browserver=$temp[1];  
  28. $temp =explode(" ",$browserver);$browserver=$temp[2];  
  29. $browserver =preg_replace("/([d.]+)/","1",$browserver);  
  30. $browserver = " $browserver";  
  31. $browser = "Opera";  
  32. }  
  33. if (ereg("Mozilla",$Agent) && ereg("MSIE",$Agent))  
  34. {  
  35. $temp = explode("(", $Agent); $Part=$temp[1];  
  36. $temp = explode(";",$Part); $Part=$temp[1];  
  37. $temp = explode(" ",$Part);$browserver=$temp[2];  
  38. $browserver =preg_replace("/([d.]+)/","1",$browserver);  
  39. $browserver = " $browserver"; //OSPHP.COM.Cn开源  
  40. $browser = "Internet Explorer";  
  41. }  //phpfensi.com 
  42. if ($browser!="")  
  43. {  
  44. $browseinfo = "$browser$browserver";  
  45. }  
  46. else  
  47. {  
  48. $browseinfo = "Unknown";  
  49. }  
  50. return $browseinfo;  
  51. }  
  52. //调用方法$browser=browseinfo() ;直接返回结果 
  53. ?> 

获取访问者操作系统 

  1. <?  
  2. function osinfo() {  
  3. $os="";  
  4. $Agent = $GLOBALS["HTTP_USER_AGENT"];  
  5. if (eregi('win',$Agent) && strpos($Agent, '95')) { //开源OSPhP.COM.CN  
  6. $os="Windows 95";  
  7. }  
  8. elseif (eregi('win 9x',$Agent) && strpos($Agent, '4.90')) {  
  9. $os="Windows ME";  
  10. }  
  11. elseif (eregi('win',$Agent) && ereg('98',$Agent)) {  
  12. $os="Windows 98";  
  13. }  
  14. elseif (eregi('win',$Agent) && eregi('nt 5.0',$Agent)) {  
  15. $os="Windows 2000";  
  16. }  
  17. elseif (eregi('win',$Agent) && eregi('nt',$Agent)) {  
  18. $os="Windows NT";  
  19. }  
  20. elseif (eregi('win',$Agent) && eregi('nt 5.1',$Agent)) {  
  21. $os="Windows XP";  
  22. }  
  23. elseif (eregi('win',$Agent) && ereg('32',$Agent)) { //PHP开源代码  
  24. $os="Windows 32";  
  25. }  
  26. elseif (eregi('linux',$Agent)) {  
  27. $os="Linux";  
  28. }  
  29. elseif (eregi('unix',$Agent)) {  
  30. $os="Unix";  
  31. }  
  32. elseif (eregi('sun',$Agent) && eregi('os',$Agent)) {  
  33. $os="SunOS";  
  34. }  
  35. elseif (eregi('ibm',$Agent) && eregi('os',$Agent)) {  
  36. $os="IBM OS/2";  
  37. }  
  38. elseif (eregi('Mac',$Agent) && eregi('PC',$Agent)) {  
  39. $os="Macintosh";  
  40. }  
  41. elseif (eregi('PowerPC',$Agent)) {  
  42. $os="PowerPC";  
  43. }  
  44. elseif (eregi('AIX',$Agent)) {  
  45. $os="AIX";  
  46. }  
  47. elseif (eregi('HPUX',$Agent)) {  
  48. $os="HPUX";  
  49. }  
  50. elseif (eregi('NetBSD',$Agent)) {  
  51. $os="NetBSD";  
  52. }  
  53. elseif (eregi('BSD',$Agent)) {  
  54. $os="BSD";  
  55. }  
  56. elseif (ereg('OSF1',$Agent)) {  
  57. $os="OSF1";  
  58. }  
  59. elseif (ereg('IRIX',$Agent)) {  
  60. $os="IRIX";  
  61. }  
  62. elseif (eregi('FreeBSD',$Agent)) {  
  63. $os="FreeBSD";  
  64. }  
  65. if ($os=='') $os = "Unknown";  
  66. return $os;  
  67. }  
  68. //调用方法$os=os_infor() ; 
  69. ?> 

文件格式类

  1. <?  
  2. $mime_types = array(  
  3. 'gif' => 'image/gif',  
  4. 'jpg' => 'image/jpeg',  
  5. 'jpeg' => 'image/jpeg',  
  6. 'jpe' => 'image/jpeg',  
  7. 'bmp' => 'image/bmp',  
  8. 'png' => 'image/png',  
  9. 'tif' => 'image/tiff', //OSPHP.COm.CN  
  10. 'tiff' => 'image/tiff',  
  11. 'pict' => 'image/x-pict',  
  12. 'pic' => 'image/x-pict',  
  13. 'pct' => 'image/x-pict',  
  14. 'tif' => 'image/tiff',  
  15. 'tiff' => 'image/tiff',  
  16. 'psd' => 'image/x-photoshop', 
  17. 'swf' => 'application/x-shockwave-flash',  
  18. 'js' => 'application/x-javascrīpt',  
  19. 'pdf' => 'application/pdf',  
  20. 'ps' => 'application/postscrīpt',  
  21. 'eps' => 'application/postscrīpt',  
  22. 'ai' => 'application/postscrīpt',  
  23. 'wmf' => 'application/x-msmetafile', 
  24. 'css' => 'text/css',  
  25. 'htm' => 'text/html',  
  26. 'html' => 'text/html',  
  27. 'txt' => 'text/plain',  
  28. 'xml' => 'text/xml',  
  29. 'wml' => 'text/wml',  
  30. 'wbmp' => 'image/vnd.wap.wbmp', 
  31. 'mid' => 'audio/midi',  
  32. 'wav' => 'audio/wav',  
  33. 'mp3' => 'audio/mpeg',  
  34. 'mp2' => 'audio/mpeg', 
  35. 'avi' => 'video/x-msvideo',  
  36. 'mpeg' => 'video/mpeg',  
  37. 'mpg' => 'video/mpeg',  
  38. 'qt' => 'video/quicktime',  
  39. 'mov' => 'video/quicktime', 
  40. 'lha' => 'application/x-lha',  
  41. 'lzh' => 'application/x-lha',  
  42. 'z' => 'application/x-compress',  
  43. 'gtar' => 'application/x-gtar',  
  44. 'gz' => 'application/x-gzip',  
  45. 'gzip' => 'application/x-gzip',  
  46. 'tgz' => 'application/x-gzip',  
  47. 'tar' => 'application/x-tar',  
  48. 'bz2' => 'application/bzip2',  
  49. 'zip' => 'application/zip',  
  50. 'arj' => 'application/x-arj',  
  51. 'rar' => 'application/x-rar-compressed', 
  52. 'hqx' => 'application/mac-binhex40',  
  53. 'sit' => 'application/x-stuffit',  
  54. 'bin' => 'application/x-macbinary', 
  55. 'uu' => 'text/x-uuencode',  
  56. 'uue' => 'text/x-uuencode', 
  57. 'latex'=> 'application/x-latex',  
  58. 'ltx' => 'application/x-latex',  
  59. 'tcl' => 'application/x-tcl', 
  60. 'pgp' => 'application/pgp',  
  61. 'asc' => 'application/pgp',  
  62. 'exe' => 'application/x-msdownload',  
  63. 'doc' => 'application/msword', //开源OSPhP.COM.CN  
  64. 'rtf' => 'application/rtf',  
  65. 'xls' => 'application/vnd.ms-excel',  
  66. 'ppt' => 'application/vnd.ms-powerpoint',  
  67. 'mdb' => 'application/x-msaccess',  
  68. 'wri' => 'application/x-mswrite',  
  69. ); 
  70. ?> 

php生成excel文档

  1. <?  
  2. header("Content-type:application/vnd.ms-excel");  
  3. header("Content-Disposition:filename=test.xls");  
  4. echo "test1t";  //phpfensi.com 
  5. echo "test2tn";  
  6. echo "test1t";  
  7. echo "test2tn";  
  8. echo "test1t";  
  9. echo "test2tn";  
  10. echo "test1t";  
  11. echo "test2tn";  
  12. echo "test1t";  
  13. echo "test2tn";  
  14. echo "test1t";  
  15. echo "test2tn";  
  16. //改动相应文件头就可以输出.doc .xls等文件格式了 
  17. ?> 

时间比较问题 

举一个简单例子说明:比如一个论坛对当天发表的贴子用new图片标记一下。 //开源OSPhP.COM.CN 

方法一:

  1. <?  
  2. //$db->rows[$i][date]中为数据库中datetime字段值.  
  3. $today=time();  
  4. $theDay=date("Y-m-d H:i:s",$today-24*3600);  
  5. $newTag=$db->rows[$i][date]>=$theDay?"<img src='../image/newinfor.gif'>":""; //OSPHP.com.CN 
  6. //方法二:  
  7. $newTag=$db->rows[$i][date]>=date("Y-m-d 00:00:00")?"<img src='../image/newinfor.gif'>":""; 
  8. ?> 
  9.  
  10. //提取页面和浏览器提交的变量,作用相当于使PHP.INI开了全局变量 //OSPHP.COm.CN  
  11. <?  
  12. @extract($_SERVER, EXTR_SKIP);  
  13. @extract($_SESSION, EXTR_SKIP);  
  14. @extract($_POST, EXTR_SKIP);  
  15. @extract($_FILES, EXTR_SKIP);  
  16. @extract($_GET, EXTR_SKIP);  
  17. @extract($_ENV, EXTR_SKIP); 
  18. ?> 
  19.  
  20. //读取文件函数  
  21. <?  
  22. function readfromfile($file_name) {  
  23. if (file_exists($file_name)) {  
  24. $filenum=fopen($file_name,"r");  
  25. flock($filenum,LOCK_EX);  
  26. $file_data=fread($filenum, filesize($file_name));  
  27. rewind($filenum); //oSPHP.COM.CN  
  28. fclose($filenum);  
  29. return $file_data;  
  30. }  
  31. } 
  32. ?> 
  33.  
  34. //写入文件函数  
  35. <?  
  36. function writetofile($file_name,$data,$method="w") {  
  37. $filenum=fopen($file_name,$method);  
  38. flock($filenum,LOCK_EX);  
  39. $file_data=fwrite($filenum,$data);  
  40. fclose($filenum);  
  41. return $file_data;  
  42. } 
  43. ?> 
  44.  //开源代码OSPhP.COm.CN  
  45. //页面快速转向  
  46. <?  
  47. function turntopage($url="index.php",$info = "页面转向中...",$second=2){  
  48. print "<html>n<head>n<title>页面转向中....</title>n";  
  49. print "<meta http-equiv="refresh" content="$second;url=$url">n";  
  50. print "<style type="text/css">n<!--n";  
  51. print "td { font-family: "Verdana", "Arial";font-size: 12px}n";  
  52. print "A {COLOR: #000000; TEXT-DECORATION: none}n";  
  53. print "-->n</style>n";  
  54. print "</head>n<body>n";  
  55. print "<table width="100%" border="0" align="center">n";  
  56. print " <tr>n";  
  57. print " <td height="200"> </td>n";  
  58. print " </tr>n";  
  59. print " <tr>n";  
  60. print " <td align="center">n";  
  61. print " <table width="60%" border="0" cellpadding="8" bgcolor="#AA9FFF">n"; //OSPHP.COm.CN  
  62. print " <tr>n";  
  63. print " <td height="30" align="center">页面转向提示信息</td>n";  
  64. print " </tr>n";  
  65. print " <tr>n";  
  66. print " <td align="center">$info</td>n";  
  67. print " </tr>n"; //OsPHP.COM.CN  
  68. print " <tr>n";  
  69. print " <td align="center">n";  
  70. print " <a href="$url">如果你的浏览器不支持自动跳转,请按这里</a></td>n";  
  71. print " </tr>n";  
  72. print " </tr>n";  
  73. print " </table></td>n";  
  74. print " </tr>n";  
  75. print " <tr>n";  
  76. print " <td height="200"> </td>n";  
  77. print " </tr>n";  
  78. print "</table>n";  
  79. print "</body>n</html>";  
  80. exit;  
  81. ?> 

产生随机字符串函数

  1. <?  
  2. function random($length) {  
  3. $hash = @#@#;  
  4. $chars = @#ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz@#;  
  5. $max = strlen($chars) - 1;  
  6. mt_srand((double)microtime() * 1000000);  
  7. for($i = 0; $i < $length; $i++) { //PHP开源代码  
  8.   $hash .= $chars[mt_rand(0, $max)];  
  9. }  
  10. return $hash;  
  11. } 
  12. ?> 

截取一定长度的字符串(该函数对GB2312使用有效)

  1. <?  
  2. function Wordscut($string, $length ,$sss=0) {  
  3. if(strlen($string) > $length) {  
  4.                if($sss){  
  5.                       $length=$length - 3;  
  6.                       $addstr=@# ...@#; //开源代码OSPhP.COm.CN  
  7.                 }  
  8.   for($i = 0; $i < $length; $i++) {  
  9.    if(ord($string[$i]) > 127) {  
  10.     $wordscut .= $string[$i].$string[$i + 1]; //oSPHP.COM.CN  
  11.     $i++;  
  12.    } else {  
  13.     $wordscut .= $string[$i];  
  14.    }  
  15.   }  
  16.   return $wordscut.$addstr;  
  17. }  
  18. return $string;  
  19. } 
  20. ?> 

取得客户端IP地址

  1. <?  
  2. function GetIP(){  
  3.              if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))  
  4.                    $ip = getenv("HTTP_CLIENT_IP");  
  5.              else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))  
  6.                    $ip = getenv("HTTP_X_FORWARDED_FOR"); //oSPHP.COM.CN  
  7.              else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))  
  8.                    $ip = getenv("REMOTE_ADDR");  
  9.              else if (isset($_SERVER[@#REMOTE_ADDR@#]) && $_SERVER[@#REMOTE_ADDR@#] && strcasecmp($_SERVER[@#REMOTE_ADDR@#], "unknown"))  
  10.                    $ip = $_SERVER[@#REMOTE_ADDR@#];  
  11.              else  
  12.                   $ip = "unknown"; 
  13.              return($ip);  
  14. } 
  15. ?>  

判断邮箱地址

  1. <?  
  2. function checkEmail($inAddress)  
  3. {  
  4. return (ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+",$inAddress));  
  5. } 
  6. ?> 

分页(两个函数配合使用)

  1. <?  
  2. function getpage($sql,$page_size=20)  
  3. {  
  4.       global $page,$totalpage,$sums;  //out param  
  5.       $page = $_GET["page"];  
  6.       //$eachpage = $page_size;  
  7.       $pagesql = strstr($sql," from ");  
  8.       $pagesql = "select count(*) as ids ".$pagesql;  
  9.       $result = mysql_query($pagesql);  
  10.       if($rs = mysql_fetch_array($result)) $sums = $rs[0];  
  11.       $totalpage = ceil($sums/$page_size);  
  12.       if((!$page)($page<1)) $page=1;  
  13.    $startpos = ($page-1)*$page_size;  
  14.    $sql .=" limit $startpos,$page_size ";  
  15.     return $sql;  
  16. }  
  17. function showbar($string="")  
  18. {       
  19.     global $page,$totalpage;  
  20. $out="共<font ".$totalpage."color=@#red@#><b>".$totalpage."</b></font>页  ";  
  21.     $linkNum =4;  
  22.     $start = ($page-round($linkNum/2))>0 ? ($page-round($linkNum/2)) : "1";  
  23.     $end   = ($page+round($linkNum/2))<$totalpage ? ($page+round($linkNum/2)) : $totalpage; 
  24.     $prestart=$start-1;  
  25.     $nextend=$end+1;  
  26.     if($page<>1)   
  27. $out .= "<a href=@#?page=1&&".$string."@#title=第一页>第一页</a> ";  
  28.     if($start>1)  
  29. $out.="<a href=@#?page=".$prestart."@# title=上一页>..<<</a> "; 
  30. for($t=$start;$t<=$end;$t++)  
  31.     {  
  32.      $out .= ($page==$t) ? "<font [".$t."]color=@#red@#><b>[".$t."]</b></font> " : "<a $thref=@#?page=$t&&".$string."@#>$t</a> ";  
  33.     }  
  34. if($end<$totalpage)  
  35. $out.="<a href=@#?page=".$nextend."&&".$string."@# title=下一页>>>..</a>";  
  36.     if($page<>$totalpage)  
  37.    $out .= " <a href=@#?page=".$totalpage."&&".$string."@# title=最后页>最后页</a>"; //开源OSPhP.COM.CN  
  38.    return $out;  
  39. } 
  40. ?> 

获取新插入数据的ID

  1. <?  
  2. mysql_insert_id();  
  3. ?> 
  4.  
  5. //获得当前的脚本网址  
  6. <?  
  7. function get_php_url(){  
  8.         if(!emptyempty($_server["REQUEST_URI"])){  
  9.                 $scriptName = $_SERVER["REQUEST_URI"];  
  10.                 $nowurl = $scriptName;  
  11.         }else{  
  12.                 $scriptName = $_SERVER["PHP_SELF"];  
  13.                 if(emptyempty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName;  
  14.                 else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];  
  15.         }  
  16.         return $nowurl;  
  17. } 
  18. ?> 
  19.  
  20. //把全角数字转为半角数字  
  21. <?  
  22. function GetAlabNum($fnum){  
  23.         $nums = array("0","1","2","3","4","5","6","7","8","9");  
  24.         $fnums = "0123456789";  
  25.         for($i=0;$i<=9;$i++) $fnum = str_replace($nums[$i],$fnums[$i],$fnum);  
  26.         $fnum = ereg_replace("[^0-9.]|^0{1,}","",$fnum);  
  27.         if($fnum=="") $fnum=0;  
  28.         return $fnum;  
  29. } 
  30. ?> 
  31.  
  32. //去除HTML标记  
  33. <?  
  34. function Text2Html($txt){  
  35.         $txt = str_replace("  "," ",$txt);  
  36.         $txt = str_replace("<","&lt;",$txt);  
  37.         $txt = str_replace(">","&gt;",$txt);  
  38.         $txt = preg_replace("/[rn]{1,}/isU","  
  39. rn",$txt);  
  40.         return $txt;  
  41. } 
  42. ?> 
  43.  
  44. //相对路径转化成绝对路径  
  45. <?  
  46. function relative_to_absolute($content, $feed_url) {   
  47.     preg_match('/(http|https|ftp):///', $feed_url, $protocol);   
  48.     $server_url = preg_replace("/(http|https|ftp|news):///", "", $feed_url);   
  49.     $server_url = preg_replace("//.*/", "", $server_url);  
  50.     if ($server_url == '') {   
  51.         return $content;   
  52.     }  
  53.     if (isset($protocol[0])) {   
  54.         $new_content = preg_replace('/href="//', 'href="'.$protocol[0].$server_url.'/', $content);   
  55.         $new_content = preg_replace('/src="//', 'src="'.$protocol[0].$server_url.'/', $new_content);   
  56.     } else {   
  57.         $new_content = $content;   
  58.     }   
  59.     return $new_content;   
  60. }  
  61. ?> 
  62.  
  63. //取得所有链接  
  64. <?  
  65. function get_all_url($code){   
  66.         preg_match_all('/<as+href=["|']?([^>"' ]+)["|']?s*[^>]*>([^>]+)</a>/i',$code,$arr);  //OsPHP.COM.CN  
  67.         return array('name'=>$arr[2],'url'=>$arr[1]);   
  68. } 
  69. ?> 
  70.  
  71. //HTML表格的每行转为CSV格式数组  
  72. <?  
  73. function get_tr_array($table) {  
  74.         $table = preg_replace("'<td[^>]*?>'si",'"',$table);  
  75.         $table = str_replace("</td>",'",',$table);  
  76.         $table = str_replace("</tr>","{tr}",$table);  
  77.         //去掉 HTML 标记   
  78.         $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);  
  79.         //去掉空白字符    
  80.         $table = preg_replace("'([rn])[s]+'","",$table);  
  81.         $table = str_replace(" ","",$table);  
  82.         $table = str_replace(" ","",$table); 
  83.         $table = explode(",{tr}",$table);  
  84.         array_pop($table);  
  85.         return $table;  
  86. } 
  87. ?> 
  88. //将HTML表格的每行每列转为数组,采集表格数据  
  89. <?  
  90. function get_td_array($table) {  
  91.         $table = preg_replace("'<table[^>]*?>'si","",$table); //开源代码OSPhP.COm.CN  
  92.         $table = preg_replace("'<tr[^>]*?>'si","",$table);  
  93.         $table = preg_replace("'<td[^>]*?>'si","",$table);  
  94.         $table = str_replace("</tr>","{tr}",$table); //开源代码OSPHP.COM.Cn  
  95.         $table = str_replace("</td>","{td}",$table);  
  96.         //去掉 HTML 标记   
  97.         $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);  
  98.         //去掉空白字符    
  99.         $table = preg_replace("'([rn])[s]+'","",$table);  
  100.         $table = str_replace(" ","",$table);  
  101.         $table = str_replace(" ","",$table);  
  102.           
  103.         $table = explode('{tr}', $table);  
  104.         array_pop($table); //OSPHP.COM.Cn开源  
  105.         foreach ($table as $key=>$tr) {  
  106.                 $td = explode('{td}', $tr);  
  107.                 array_pop($td);  
  108.             $td_array[] = $td;  
  109.         }  
  110.         return $td_array;  
  111. } 
  112. ?> 
  113.  
  114. //返回字符串中的所有单词 $distinct=true 去除重复  
  115. <?  
  116. function split_en_str($str,$distinct=true) {  
  117.         preg_match_all('/([a-zA-Z]+)/',$str,$match);  
  118.         if ($distinct == true) {  
  119.                 $match[1] = array_unique($match[1]);  
  120.         }  
  121.         sort($match[1]);  
  122.         return $match[1];  
  123. }  
  124. ?> 
  125.  
  126. //打印出为本PHP项目做出贡献的人员的清单  
  127. <?  
  128. string phpcredits(void) 
  129. ?> 

Tags: OpenCart php支付宝 php支付参数

分享到: