chenjiali 发表于 2015-8-26 15:52:20

php常用函数 精选

   
<?      
function GetIP() { //获取IP      
if ($_SERVER[&quot;HTTP_X_FORWARDED_FOR&quot;])      
$ip = $_SERVER[&quot;HTTP_X_FORWARDED_FOR&quot;]; //OSPHP.com.CN      
    else if ($_SERVER[&quot;HTTP_CLIENT_IP&quot;])      
$ip = $_SERVER[&quot;HTTP_CLIENT_IP&quot;];      
    else if ($_SERVER[&quot;REMOTE_ADDR&quot;])      
$ip = $_SERVER[&quot;REMOTE_ADDR&quot;]; //OsPHP.COM.CN      
    else if (getenv(&quot;HTTP_X_FORWARDED_FOR&quot;))      
$ip = getenv(&quot;HTTP_X_FORWARDED_FOR&quot;);      
    else if (getenv(&quot;HTTP_CLIENT_IP&quot;))      
$ip = getenv(&quot;HTTP_CLIENT_IP&quot;); //PHP开源代码      
    else if (getenv(&quot;REMOTE_ADDR&quot;))      
$ip = getenv(&quot;REMOTE_ADDR&quot;);      
    else      
$ip = &quot;Unknown&quot;;      
    return $ip;      
}      
?>
  
//开源代码OSPHP.COM.Cn   
<?php      
function DateAdd($date, $int, $unit = &quot;d&quot;) { //时间的增加(还可以改进成时分秒都可以增加,有时间再补上)      
$dateArr = explode(&quot;-&quot;, $date);      
$value[$unit] = $int;     //OsPHP.COM.CN
  return date(&quot;Y-m-d&quot;, mktime(0,0,0, $dateArr + $value['m'], $dateArr + $value['d'], $dateArr + $value['y']));
  //开源OSPhP.COM.CN
  }      
function GetWeekDay($date) {//计算出给出的日期是星期几      
$dateArr = explode(&quot;-&quot;, $date);      
    return date(&quot;w&quot;, mktime(0,0,0,$dateArr,$dateArr,$dateArr));
  //开源代码OSPhP.COm.CN
  }      
?>      


   
<?      
function check_date($date) { //检查日期是否合法日期      
$dateArr = explode(&quot;-&quot;, $date);      
    if (is_numeric($dateArr) && is_numeric($dateArr) && is_numeric($dateArr)) { //开源OSPhP.COM.CN      
      return checkdate($dateArr,$dateArr,$dateArr);      
    }      
    return false;      
}      
function check_time($time) {//检查时间是否合法时间     //OSPHP.COM.Cn开源
  $timeArr = explode(&quot;:&quot;, $time);      
    if (is_numeric($timeArr) && is_numeric($timeArr) && is_numeric($timeArr)) {
  //开源代码OSPHP.COM.Cn
  if (($timeArr >= 0 && $timeArr <= 23) && ($timeArr >= 0 && $timeArr <= 59) && ($timeArr >= 0 && $timeArr <= 59))
  //PHP开源代码
  return true;      
      else      
            return false;      
    }      
    return false;      
}      
function DateDiff($date1, $date2, $unit = &quot;&quot;) { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天 //oSPHP.COM.CN      
switch ($unit) {      
      case 's':      
$dividend = 1;      
            break;      
      case 'i':      
$dividend = 60;
  //oSPHP.COM.CN
  break;      
      case 'h':      
$dividend = 3600;      
            break;      
      case 'd':      
$dividend = 86400;      
            break; //开源代码OSPhP.COm.CN      
      default:      
$dividend = 86400;      
    }      
$time1 = strtotime($date1);      
$time2 = strtotime($date2);      
    if ($time1 && $time2) //OSPHP.com.CN      
      return (float)($time1 - $time2) / $dividend;      
    return false;      
}      
?>      


   
PHP重定向      
<?      
方法一:header(&quot;Location: index.php&quot;);      
方法二:echo &quot;<scrīpt>window.location =&quot;$PHP_SELF&quot;;</scrīpt>&quot;;     //开源代码OSPhP.COm.CN
  方法三:echo &quot;<META HTTP-EQUIV=&quot;Refresh&quot; CONTENT=&quot;0; URL=index.php&quot;>&quot;;      
?>      


   
获取访问者浏览器      
<?      
function browse_infor()      
{      
$browser=&quot;&quot;;$browserver=&quot;&quot;;     //OSPHP.COm.CN
  $Browsers =array(&quot;Lynx&quot;,&quot;MOSAIC&quot;,&quot;AOL&quot;,&quot;Opera&quot;,&quot;JAVA&quot;,&quot;MacWeb&quot;,&quot;WebExplorer&quot;,&quot;OmniWeb&quot;);      
$Agent = $GLOBALS[&quot;HTTP_USER_AGENT&quot;];      
for ($i=0; $i<=7; $i++) //开源代码OSPHP.COM.Cn      
{      
if (strpos($Agent,$Browsers[$i]))      
{      
$browser = $Browsers[$i];      
$browserver =&quot;&quot;;      
}      
}      
if (ereg(&quot;Mozilla&quot;,$Agent) && !ereg(&quot;MSIE&quot;,$Agent)) //开源代码OSPhP.COm.CN      
{      
$temp =explode(&quot;(&quot;, $Agent); $Part=$temp;      
$temp =explode(&quot;/&quot;, $Part); $browserver=$temp;
  //OSPHP.COM.Cn开源
  $temp =explode(&quot; &quot;,$browserver); $browserver=$temp;      
$browserver =preg_replace(&quot;/(+)/&quot;,&quot;1&quot;,$browserver);      
$browserver = &quot; $browserver&quot;; //OsPHP.COM.CN      
$browser = &quot;Netscape Navigator&quot;;      
}      
if (ereg(&quot;Mozilla&quot;,$Agent) && ereg(&quot;Opera&quot;,$Agent))      
{      
$temp =explode(&quot;(&quot;, $Agent); $Part=$temp;
  //PHP开源代码
  $temp =explode(&quot;)&quot;, $Part); $browserver=$temp;      
$temp =explode(&quot; &quot;,$browserver);$browserver=$temp;
  //开源代码OSPHP.COM.Cn
  $browserver =preg_replace(&quot;/(+)/&quot;,&quot;1&quot;,$browserver);      
$browserver = &quot; $browserver&quot;;      
$browser = &quot;Opera&quot;;      
}      
if (ereg(&quot;Mozilla&quot;,$Agent) && ereg(&quot;MSIE&quot;,$Agent))
  //PHP开源代码
  {      
$temp = explode(&quot;(&quot;, $Agent); $Part=$temp;      
$temp = explode(&quot;;&quot;,$Part); $Part=$temp; //OsPHP.COM.CN      
$temp = explode(&quot; &quot;,$Part);$browserver=$temp;      
$browserver =preg_replace(&quot;/(+)/&quot;,&quot;1&quot;,$browserver);      
$browserver = &quot; $browserver&quot;; //OSPHP.com.CN      
$browser = &quot;Internet Explorer&quot;;      
}      
if ($browser!=&quot;&quot;)      
{      
$browseinfo = &quot;$browser$browserver&quot;;      
}      
else      
{      
$browseinfo = &quot;Unknown&quot;;      
}      
return $browseinfo;      
}      
//调用方法$browser=browseinfo() ;直接返回结果      
?> //开源OSPhP.COM.CN      


   
获取访问者操作系统      
<?      
function osinfo() {      
$os=&quot;&quot;;      
$Agent = $GLOBALS[&quot;HTTP_USER_AGENT&quot;];      
if (eregi('win',$Agent) && strpos($Agent, '95')) { //OsPHP.COM.CN      
$os=&quot;Windows 95&quot;;      
}      
elseif (eregi('win 9x',$Agent) && strpos($Agent, '4.90')) {      
$os=&quot;Windows ME&quot;;      
}      
elseif (eregi('win',$Agent) && ereg('98',$Agent)) {     //开源OSPhP.COM.CN
  $os=&quot;Windows 98&quot;;      
}      
elseif (eregi('win',$Agent) && eregi('nt 5.0',$Agent)) {      
$os=&quot;Windows 2000&quot;;      
}      
elseif (eregi('win',$Agent) && eregi('nt',$Agent)) {
  //OSPHP.COM.Cn开源
  $os=&quot;Windows NT&quot;;      
}      
elseif (eregi('win',$Agent) && eregi('nt 5.1',$Agent)) {      
$os=&quot;Windows XP&quot;;      
}      
elseif (eregi('win',$Agent) && ereg('32',$Agent)) {
  //开源OSPhP.COM.CN
  $os=&quot;Windows 32&quot;;      
}      
elseif (eregi('linux',$Agent)) {      
$os=&quot;Linux&quot;;      
}      
elseif (eregi('unix',$Agent)) {      
$os=&quot;Unix&quot;; //PHP开源代码      
}      
elseif (eregi('sun',$Agent) && eregi('os',$Agent)) {      
$os=&quot;SunOS&quot;;      
}      
elseif (eregi('ibm',$Agent) && eregi('os',$Agent)) { //OSPHP.COm.CN      
$os=&quot;IBM OS/2&quot;;      
}      
elseif (eregi('Mac',$Agent) && eregi('PC',$Agent)) {      
$os=&quot;Macintosh&quot;;      
}      
elseif (eregi('PowerPC',$Agent)) { //OSPHP.com.CN      
$os=&quot;PowerPC&quot;;      
}      
elseif (eregi('AIX',$Agent)) {      
$os=&quot;AIX&quot;;      
}      
elseif (eregi('HPUX',$Agent)) {      
$os=&quot;HPUX&quot;;
  //开源代码OSPhP.COm.CN
  }      
elseif (eregi('NetBSD',$Agent)) {      
$os=&quot;NetBSD&quot;;      
}      
elseif (eregi('BSD',$Agent)) {      
$os=&quot;BSD&quot;;      
}      
elseif (ereg('OSF1',$Agent)) {
  //开源代码OSPHP.COM.Cn
  $os=&quot;OSF1&quot;;      
}      
elseif (ereg('IRIX',$Agent)) {      
$os=&quot;IRIX&quot;;      
}      
elseif (eregi('FreeBSD',$Agent)) {      
$os=&quot;FreeBSD&quot;;
  //oSPHP.COM.CN
  }      
if ($os=='') $os = &quot;Unknown&quot;;      
return $os;      
}      
//调用方法$os=os_infor() ;      
?>      


   
文件格式类      
<?      
$mime_types = array(      
'gif' => 'image/gif', //oSPHP.COM.CN      
'jpg' => 'image/jpeg',      
'jpeg' => 'image/jpeg',      
'jpe' => 'image/jpeg',      
'bmp' => 'image/bmp',      
'png' => 'image/png',      
'tif' => 'image/tiff', //开源代码OSPHP.COM.Cn      
'tiff' => 'image/tiff',      
'pict' => 'image/x-pict',      
'pic' => 'image/x-pict',      
'pct' => 'image/x-pict',      
'tif' => 'image/tiff',      
'tiff' => 'image/tiff', //oSPHP.COM.CN      
'psd' => 'image/x-photoshop',      
'swf' => 'application/x-shockwave-flash',      
'js' => 'application/x-javascrīpt',      
'pdf' => 'application/pdf',      
'ps' => 'application/postscrīpt',      
'eps' => 'application/postscrīpt', //开源OSPhP.COM.CN      
'ai' => 'application/postscrīpt',      
'wmf' => 'application/x-msmetafile',      
'css' => 'text/css',      
'htm' => 'text/html',      
'html' => 'text/html',      
'txt' => 'text/plain', //开源OSPhP.COM.CN      
'xml' => 'text/xml',      
'wml' => 'text/wml',      
'wbmp' => 'image/vnd.wap.wbmp',      
'mid' => 'audio/midi',      
'wav' => 'audio/wav',      
'mp3' => 'audio/mpeg', //OSPHP.com.CN      
'mp2' => 'audio/mpeg',      
'avi' => 'video/x-msvideo',      
'mpeg' => 'video/mpeg',      
'mpg' => 'video/mpeg',      
'qt' => 'video/quicktime',      
'mov' => 'video/quicktime', //开源OSPhP.COM.CN      
'lha' => 'application/x-lha',      
'lzh' => 'application/x-lha',      
'z' => 'application/x-compress',      
'gtar' => 'application/x-gtar',      
'gz' => 'application/x-gzip',      
'gzip' => 'application/x-gzip',     //OSPHP.COM.Cn开源
  'tgz' => 'application/x-gzip',      
'tar' => 'application/x-tar',      
'bz2' => 'application/bzip2',      
'zip' => 'application/zip',      
'arj' => 'application/x-arj',      
'rar' => 'application/x-rar-compressed',
  //开源代码OSPHP.COM.Cn
  'hqx' => 'application/mac-binhex40',      
'sit' => 'application/x-stuffit',      
'bin' => 'application/x-macbinary',      
'uu' => 'text/x-uuencode',      
'uue' => 'text/x-uuencode',      
'latex'=> 'application/x-latex',
  //OSPHP.COM.Cn开源
  'ltx' => 'application/x-latex',      
'tcl' => 'application/x-tcl',      
'pgp' => 'application/pgp',      
'asc' => 'application/pgp',      
'exe' => 'application/x-msdownload',      
'doc' => 'application/msword',
  //开源OSPhP.COM.CN
  'rtf' => 'application/rtf',      
'xls' => 'application/vnd.ms-excel',      
'ppt' => 'application/vnd.ms-powerpoint',      
'mdb' => 'application/x-msaccess',      
'wri' => 'application/x-mswrite',      
);      
?> //OSPHP.com.CN      


   
php生成excel文档      
<?      
header(&quot;Content-type:application/vnd.ms-excel&quot;);      
header(&quot;Content-Disposition:filename=test.xls&quot;);      
echo &quot;test1t&quot;;      
echo &quot;test2tn&quot;;      
echo &quot;test1t&quot;;      
echo &quot;test2tn&quot;;      
echo &quot;test1t&quot;;     //PHP开源代码
  echo &quot;test2tn&quot;;      
echo &quot;test1t&quot;;      
echo &quot;test2tn&quot;;      
echo &quot;test1t&quot;;      
echo &quot;test2tn&quot;;      
echo &quot;test1t&quot;;      
echo &quot;test2tn&quot;;      
//改动相应文件头就可以输出.doc .xls等文件格式了      
?>      


   
时间比较问题      
举一个简单例子说明:比如一个论坛对当天发表的贴子用new图片标记一下。 //PHP开源代码      
方法一:      
<?      
//$db->rows[$i]中为数据库中datetime字段值.      
$today=time();      
$theDay=date(&quot;Y-m-d H:i:s&quot;,$today-24*3600);      
$newTag=$db->rows[$i]>=$theDay?&quot;<img src='../image/newinfor.gif'>&quot;:&quot;&quot;;     //oSPHP.COM.CN
  //方法二:      
$newTag=$db->rows[$i]>=date(&quot;Y-m-d 00:00:00&quot;)?&quot;<img src='../image/newinfor.gif'>&quot;:&quot;&quot;;      
?>      


   
//提取页面和浏览器提交的变量,作用相当于使PHP.INI开了全局变量 //OSPHP.com.CN      
<?      
@extract($_SERVER, EXTR_SKIP);      
@extract($_SESSION, EXTR_SKIP);      
@extract($_POST, EXTR_SKIP);      
@extract($_FILES, EXTR_SKIP); //开源OSPhP.COM.CN      
@extract($_GET, EXTR_SKIP);      
@extract($_ENV, EXTR_SKIP);      
?>      

   
//读取文件函数      
<?      
function readfromfile($file_name) {      
if (file_exists($file_name)) {     //OSPHP.COM.Cn开源
  $filenum=fopen($file_name,&quot;r&quot;);      
flock($filenum,LOCK_EX);      
$file_data=fread($filenum, filesize($file_name));      
rewind($filenum); //OSPHP.COm.CN      
fclose($filenum);      
return $file_data;      
}      
}      
?>      


   
//写入文件函数      
<?      
function writetofile($file_name,$data,$method=&quot;w&quot;) {      
$filenum=fopen($file_name,$method);     //OSPHP.COm.CN
  flock($filenum,LOCK_EX);      
$file_data=fwrite($filenum,$data);      
fclose($filenum);      
return $file_data;      
}      
?>      


      //开源代码OSPHP.COM.Cn
  //页面快速转向      
<?      
function turntopage($url=&quot;index.php&quot;,$info = &quot;页面转向中...&quot;,$second=2){      
print &quot;<html>n<head>n<title>页面转向中....</title>n&quot;;      
print &quot;<meta http-equiv=&quot;refresh&quot; content=&quot;$second;url=$url&quot;>n&quot;; //开源代码OSPHP.COM.Cn      
print &quot;<style type=&quot;text/css&quot;>n<!--n&quot;;      
print &quot;td { font-family: &quot;Verdana&quot;, &quot;Arial&quot;;font-size: 12px}n&quot;;      
print &quot;A {COLOR: #000000; TEXT-DECORATION: none}n&quot;;      
print &quot;-->n</style>n&quot;;      
print &quot;</head>n<body>n&quot;;      
print &quot;<table width=&quot;100%&quot; border=&quot;0&quot; align=&quot;center&quot;>n&quot;;
  //oSPHP.COM.CN
  print &quot; <tr>n&quot;;      
print &quot; <td height=&quot;200&quot;> </td>n&quot;;      
print &quot; </tr>n&quot;;      
print &quot; <tr>n&quot;;      
print &quot; <td align=&quot;center&quot;>n&quot;;      
print &quot; <table width=&quot;60%&quot; border=&quot;0&quot; cellpadding=&quot;8&quot; bgcolor=&quot;#AA9FFF&quot;>n&quot;;
  //开源代码OSPHP.COM.Cn
  print &quot; <tr>n&quot;;      
print &quot; <td height=&quot;30&quot; align=&quot;center&quot;>页面转向提示信息</td>n&quot;;      
print &quot; </tr>n&quot;;      
print &quot; <tr>n&quot;;      
print &quot; <td align=&quot;center&quot;>$info</td>n&quot;;      
print &quot; </tr>n&quot;;      
print &quot; <tr>n&quot;;
  //OSPHP.com.CN
  print &quot; <td align=&quot;center&quot;>n&quot;;      
print &quot; <a href=&quot;$url&quot;>如果你的浏览器不支持自动跳转,请按这里</a></td>n&quot;;      
print &quot; </tr>n&quot;;      
print &quot; </tr>n&quot;;      
print &quot; </table></td>n&quot;;      
print &quot; </tr>n&quot;;      
print &quot; <tr>n&quot;; //开源OSPhP.COM.CN      
print &quot; <td height=&quot;200&quot;> </td>n&quot;;      
print &quot; </tr>n&quot;;      
print &quot;</table>n&quot;;      
print &quot;</body>n</html>&quot;;      
exit;      
?>

   
产生随机字符串函数      
<?      
function random($length) {      
$hash = @#@#;      
$chars = @#ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz@#; //oSPHP.COM.CN      
$max = strlen($chars) - 1;      
mt_srand((double)microtime() * 1000000);      
for($i = 0; $i < $length; $i++) {     //开源代码OSPHP.COM.Cn
  $hash .= $chars;      
}      
return $hash;      
}      
?>      


   
截取一定长度的字符串(该函数对GB2312使用有效)      
<?      
function Wordscut($string, $length ,$sss=0) {     //OSPHP.com.CN
  if(strlen($string) > $length) {      
               if($sss){      
$length=$length - 3;      
$addstr=@# ...@#;
  //OSPHP.COM.Cn开源
  }      
for($i = 0; $i < $length; $i++) {      
   if(ord($string[$i]) > 127) {      
$wordscut .= $string[$i].$string[$i + 1]; //oSPHP.COM.CN      
$i++;      
   } else {      
$wordscut .= $string[$i];      
   }      
}      
return $wordscut.$addstr;      
}      
return $string;      
}      
?>      


   
取得客户端IP地址      
<?     //开源代码OSPhP.COm.CN
  function GetIP(){      
             if (getenv(&quot;HTTP_CLIENT_IP&quot;) && strcasecmp(getenv(&quot;HTTP_CLIENT_IP&quot;), &quot;unknown&quot;))      
$ip = getenv(&quot;HTTP_CLIENT_IP&quot;); //OSPHP.COM.Cn开源      
             else if (getenv(&quot;HTTP_X_FORWARDED_FOR&quot;) && strcasecmp(getenv(&quot;HTTP_X_FORWARDED_FOR&quot;), &quot;unknown&quot;))      
$ip = getenv(&quot;HTTP_X_FORWARDED_FOR&quot;); //OSPHP.com.CN      
             else if (getenv(&quot;REMOTE_ADDR&quot;) && strcasecmp(getenv(&quot;REMOTE_ADDR&quot;), &quot;unknown&quot;))      
$ip = getenv(&quot;REMOTE_ADDR&quot;);      
             else if (isset($_SERVER[@#REMOTE_ADDR@#]) && $_SERVER[@#REMOTE_ADDR@#] && strcasecmp($_SERVER[@#REMOTE_ADDR@#], &quot;unknown&quot;))
  //OSPHP.COm.CN
  $ip = $_SERVER[@#REMOTE_ADDR@#];      
else      
$ip = &quot;unknown&quot;;      
             return($ip);      
}      
?>      


  //开源代码OSPhP.COm.CN
   
判断邮箱地址      
<?      
function checkEmail($inAddress)      
{      
return (ereg(&quot;^()+@()+(.)+&quot;,$inAddress));      
}      
?>      

   
分页(两个函数配合使用)      
<?      
function getpage($sql,$page_size=20)     //开源代码OSPhP.COm.CN
  {      
      global $page,$totalpage,$sums;//out param      
$page = $_GET[&quot;page&quot;];      
//$eachpage = $page_size;      
$pagesql = strstr($sql,&quot; from &quot;);
  //oSPHP.COM.CN
  $pagesql = &quot;select count(*) as ids &quot;.$pagesql;      
$result = mysql_query($pagesql);      
      if($rs = mysql_fetch_array($result)) $sums = $rs;
  //PHP开源代码
  $totalpage = ceil($sums/$page_size);      
      if((!$page)($page<1)) $page=1;      
$startpos = ($page-1)*$page_size; //OsPHP.COM.CN      
$sql .=&quot; limit $startpos,$page_size &quot;;      
    return $sql;      
}      
function showbar($string=&quot;&quot;)      
{      
    global $page,$totalpage;      
$out=&quot;共<font &quot;.$totalpage.&quot;color=@#red@#><b>&quot;.$totalpage.&quot;</b></font>页&quot;; //OsPHP.COM.CN      
$linkNum =4;      
$start = ($page-round($linkNum/2))>0 ? ($page-round($linkNum/2)) : &quot;1&quot;; //OSPHP.com.CN      
$end   = ($page+round($linkNum/2))<$totalpage ? ($page+round($linkNum/2)) : $totalpage;      
$prestart=$start-1; //OSPHP.com.CN      
$nextend=$end+1;      
    if($page<>1)
$out .= &quot;<a href=@#?page=1&&&quot;.$string.&quot;@#title=第一页>第一页</a> &quot;;      
    if($start>1) //OSPHP.COm.CN      
$out.=&quot;<a href=@#?page=&quot;.$prestart.&quot;@# title=上一页>..<<</a> &quot;;      
for($t=$start;$t<=$end;$t++)      
    {      
$out .= ($page==$t) ? &quot;<font [&quot;.$t.&quot;]color=@#red@#><b>[&quot;.$t.&quot;]</b></font> &quot; : &quot;<a $thref=@#?page=$t&&&quot;.$string.&quot;@#>$t</a> &quot;; //oSPHP.COM.CN      
    }      
if($end<$totalpage)      
$out.=&quot;<a href=@#?page=&quot;.$nextend.&quot;&&&quot;.$string.&quot;@# title=下一页>>>..</a>&quot;;      
    if($page<>$totalpage)      
$out .= &quot; <a href=@#?page=&quot;.$totalpage.&quot;&&&quot;.$string.&quot;@# title=最后页>最后页</a>&quot;; //OSPHP.COM.Cn开源      
   return $out;      
}      
?>      


   
获取新插入数据的ID      
<?      
mysql_insert_id();      
?>      

   
//获得当前的脚本网址      
<?      
function get_php_url(){      
      if(!empty($_server[&quot;REQUEST_URI&quot;])){     //开源代码OSPhP.COm.CN
  $scriptName = $_SERVER[&quot;REQUEST_URI&quot;];      
$nowurl = $scriptName;      
      }else{      
$scriptName = $_SERVER[&quot;PHP_SELF&quot;]; //OsPHP.COM.CN      
                if(empty($_SERVER[&quot;QUERY_STRING&quot;])) $nowurl = $scriptName;      
                else $nowurl = $scriptName.&quot;?&quot;.$_SERVER[&quot;QUERY_STRING&quot;];      
      }
  //OsPHP.COM.CN
  return $nowurl;      
}      
?>      


   
//把全角数字转为半角数字      
<?      
function GetAlabNum($fnum){      
$nums = array(&quot;0&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;7&quot;,&quot;8&quot;,&quot;9&quot;);     //OSPHP.com.CN
  $fnums = &quot;0123456789&quot;;      
      for($i=0;$i<=9;$i++) $fnum = str_replace($nums[$i],$fnums[$i],$fnum); //OsPHP.COM.CN      
$fnum = ereg_replace(&quot;[^0-9.]|^0{1,}&quot;,&quot;&quot;,$fnum);      
      if($fnum==&quot;&quot;) $fnum=0;      
      return $fnum;      
}      
?>
  //PHP开源代码
  

   
//去除HTML标记      
<?      
function Text2Html($txt){      
$txt = str_replace(&quot;&quot;,&quot; &quot;,$txt);      
$txt = str_replace(&quot;<&quot;,&quot;&lt;&quot;,$txt); //开源OSPhP.COM.CN      
$txt = str_replace(&quot;>&quot;,&quot;&gt;&quot;,$txt);      
$txt = preg_replace(&quot;/{1,}/isU&quot;,&quot;      
rn&quot;,$txt);      
      return $txt;     //OSPHP.COm.CN
  }      
?>      


   
//相对路径转化成绝对路径      
<?      
function relative_to_absolute($content, $feed_url) {
preg_match('/(http|https|ftp):///', $feed_url, $protocol);
$server_url = preg_replace(&quot;/(http|https|ftp|news):///&quot;, &quot;&quot;, $feed_url);        //开源OSPhP.COM.CN
  $server_url = preg_replace(&quot;//.*/&quot;, &quot;&quot;, $server_url);
    if ($server_url == '') {
      return $content;
    }
    if (isset($protocol)) {
  //开源代码OSPhP.COm.CN
  $new_content = preg_replace('/href=&quot;//', 'href=&quot;'.$protocol.$server_url.'/', $content);
$new_content = preg_replace('/src=&quot;//', 'src=&quot;'.$protocol.$server_url.'/', $new_content);//开源OSPhP.COM.CN      
    } else {
$new_content = $content;
    }
    return $new_content;
}
?>      


   
//取得所有链接      
<?      
function get_all_url($code){
preg_match_all('/<as+href=[&quot;|']?([^>&quot;' ]+)[&quot;|']?s*[^>]*>([^>]+)</a>/i',$code,$arr);//OSPHP.COm.CN      
      return array('name'=>$arr,'url'=>$arr);
}      
?>      

   
//HTML表格的每行转为CSV格式数组      
<?      
function get_tr_array($table) {     //PHP开源代码
  $table = preg_replace(&quot;'<td[^>]*?>'si&quot;,'&quot;',$table);      
$table = str_replace(&quot;</td>&quot;,'&quot;,',$table);      
$table = str_replace(&quot;</tr>&quot;,&quot;{tr}&quot;,$table); //开源代码OSPhP.COm.CN      
//去掉 HTML 标记
$table = preg_replace(&quot;'<[/!]*?[^<>]*?>'si&quot;,&quot;&quot;,$table);      
//去掉空白字符   
$table = preg_replace(&quot;'()+'&quot;,&quot;&quot;,$table);
  //开源代码OSPhP.COm.CN
  $table = str_replace(&quot; &quot;,&quot;&quot;,$table);      
$table = str_replace(&quot; &quot;,&quot;&quot;,$table);      
$table = explode(&quot;,{tr}&quot;,$table);
  //开源代码OSPHP.COM.Cn
  array_pop($table);      
      return $table;      
}      
?>

   
//将HTML表格的每行每列转为数组,采集表格数据      
<?      
function get_td_array($table) {      
$table = preg_replace(&quot;'<table[^>]*?>'si&quot;,&quot;&quot;,$table);     //OSPHP.COm.CN
  $table = preg_replace(&quot;'<tr[^>]*?>'si&quot;,&quot;&quot;,$table);      
$table = preg_replace(&quot;'<td[^>]*?>'si&quot;,&quot;&quot;,$table);      
$table = str_replace(&quot;</tr>&quot;,&quot;{tr}&quot;,$table); //开源代码OSPhP.COm.CN      
$table = str_replace(&quot;</td>&quot;,&quot;{td}&quot;,$table);      
//去掉 HTML 标记
$table = preg_replace(&quot;'<[/!]*?[^<>]*?>'si&quot;,&quot;&quot;,$table); //OSPHP.COM.Cn开源      
//去掉空白字符   
$table = preg_replace(&quot;'()+'&quot;,&quot;&quot;,$table);      
$table = str_replace(&quot; &quot;,&quot;&quot;,$table);
  //开源代码OSPHP.COM.Cn
  $table = str_replace(&quot; &quot;,&quot;&quot;,$table);      
$table = explode('{tr}', $table);      
array_pop($table); //PHP开源代码      
      foreach ($table as $key=>$tr) {      
$td = explode('{td}', $tr);      
array_pop($td);      
$td_array[] = $td; //PHP开源代码      
      }      
      return $td_array;      
}      
?>      


   
//返回字符串中的所有单词 $distinct=true 去除重复      
<?      
function split_en_str($str,$distinct=true) {      
preg_match_all('/(+)/',$str,$match);     //开源代码OSPHP.COM.Cn
  if ($distinct == true) {      
$match = array_unique($match);      
      }      
sort($match); //OSPHP.COm.CN      
      return $match;      
}      
?>      


   
//打印出为本PHP项目做出贡献的人员的清单      
<?      
string phpcredits(void)      
?>
页: [1]
查看完整版本: php常用函数 精选