当前位置:Gxlcms > PHP基础 > 收藏的PHP常用函数 推荐收藏保存

收藏的PHP常用函数 推荐收藏保存

时间:2021-07-01 10:21:17 帮助过:27人阅读

内容:
代码如下:
  1. <br><? <br>function GetIP() { //获取IP <br>if ($_SERVER["HTTP_X_FORWARDED_FOR"]) <br>$ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; <br>else if ($_SERVER["HTTP_CLIENT_IP"]) <br>$ip = $_SERVER["HTTP_CLIENT_IP"]; <br>else if ($_SERVER["REMOTE_ADDR"]) <br>$ip = $_SERVER["REMOTE_ADDR"]; <br>else if (getenv("HTTP_X_FORWARDED_FOR")) <br>$ip = getenv("HTTP_X_FORWARDED_FOR"); <br>else if (getenv("HTTP_CLIENT_IP")) <br>$ip = getenv("HTTP_CLIENT_IP"); <br>else if (getenv("REMOTE_ADDR")) <br>$ip = getenv("REMOTE_ADDR"); <br>else <br>$ip = "Unknown"; <br>return $ip; <br>} <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><?php <br>function DateAdd($date, $int, $unit = "d") { //时间的增加(还可以改进成时分秒都可以增加,有时间再补上) <br>$dateArr = explode("-", $date); <br>$value[$unit] = $int; <br>return date("Y-m-d", mktime(0,0,0, $dateArr[1] + $value['m'], $dateArr[2] + $value['d'], $dateArr[0] + $value['y'])); <br>} <br>function GetWeekDay($date) { //计算出给出的日期是星期几 <br>$dateArr = explode("-", $date); <br>return date("w", mktime(0,0,0,$dateArr[1],$dateArr[2],$dateArr[0])); <br>} <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>function check_date($date) { //检查日期是否合法日期 <br>$dateArr = explode("-", $date); <br>if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) { <br>return checkdate($dateArr[1],$dateArr[2],$dateArr[0]); <br>} <br>return false; <br>} <br>function check_time($time) { //检查时间是否合法时间 <br>$timeArr = explode(":", $time); <br>if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) { <br>if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59)) <br>return true; <br>else <br>return false; <br>} <br>return false; <br>} <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>function DateDiff($date1, $date2, $unit = "") { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天 <br>switch ($unit) { <br>case 's': <br>$dividend = 1; <br>break; <br>case 'i': <br>$dividend = 60; <br>break; <br>case 'h': <br>$dividend = 3600; <br>break; <br>case 'd': <br>$dividend = 86400; <br>break; <br>default: <br>$dividend = 86400; <br>} <br>$time1 = strtotime($date1); <br>$time2 = strtotime($date2); <br>if ($time1 && $time2) <br>return (float)($time1 - $time2) / $dividend; <br>return false; <br>} <br>?> <br> <br>PHP重定向 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>方法一:header("Location: index.php"); <br>方法二:echo "<scrīpt>window.location ="$PHP_SELF";</scrīpt>"; <br>方法三:echo "<META HTTP-EQUIV="Refresh" CONTENT="0; URL=index.php">"; <br>?> <br> <br>获取访问者浏览器 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>function browse_infor() <br>{ <br>$browser="";$browserver=""; <br>$Browsers =array("Lynx","MOSAIC","AOL","Opera","JAVA","MacWeb","WebExplorer","OmniWeb"); <br>$Agent = $GLOBALS["HTTP_USER_AGENT"]; <br>for ($i=0; $i<=7; $i++) <br>{ <br>if (strpos($Agent,$Browsers[$i])) <br>{ <br>$browser = $Browsers[$i]; <br>$browserver =""; <br>} <br>} <br>if (ereg("Mozilla",$Agent) && !ereg("MSIE",$Agent)) <br>{ <br>$temp =explode("(", $Agent); $Part=$temp[0]; <br>$temp =explode("/", $Part); $browserver=$temp[1]; <br>$temp =explode(" ",$browserver); $browserver=$temp[0]; <br>$browserver =preg_replace("/([d.]+)/","1",$browserver); <br>$browserver = " $browserver"; <br>$browser = "Netscape Navigator"; <br>} <br>if (ereg("Mozilla",$Agent) && ereg("Opera",$Agent)) <br>{ <br>$temp =explode("(", $Agent); $Part=$temp[1]; <br>$temp =explode(")", $Part); $browserver=$temp[1]; <br>$temp =explode(" ",$browserver);$browserver=$temp[2]; <br>$browserver =preg_replace("/([d.]+)/","1",$browserver); <br>$browserver = " $browserver"; <br>$browser = "Opera"; <br>} <br>if (ereg("Mozilla",$Agent) && ereg("MSIE",$Agent)) <br>{ <br>$temp = explode("(", $Agent); $Part=$temp[1]; <br>$temp = explode(";",$Part); $Part=$temp[1]; <br>$temp = explode(" ",$Part);$browserver=$temp[2]; <br>$browserver =preg_replace("/([d.]+)/","1",$browserver); <br>$browserver = " $browserver"; <br>$browser = "Internet Explorer"; <br>} <br>if ($browser!="") <br>{ <br>$browseinfo = "$browser$browserver"; <br>} <br>else <br>{ <br>$browseinfo = "Unknown"; <br>} <br>return $browseinfo; <br>} <br>//调用方法$browser=browseinfo() ;直接返回结果 <br>?> <br> <br>获取访问者操作系统 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>function osinfo() { <br>$os=""; <br>$Agent = $GLOBALS["HTTP_USER_AGENT"]; <br>if (eregi('win',$Agent) && strpos($Agent, '95')) { <br>$os="Windows 95"; <br>} <br>elseif (eregi('win 9x',$Agent) && strpos($Agent, '4.90')) { <br>$os="Windows ME"; <br>} <br>elseif (eregi('win',$Agent) && ereg('98',$Agent)) { <br>$os="Windows 98"; <br>} <br>elseif (eregi('win',$Agent) && eregi('nt 5.0',$Agent)) { <br>$os="Windows 2000"; <br>} <br>elseif (eregi('win',$Agent) && eregi('nt',$Agent)) { <br>$os="Windows NT"; <br>} <br>elseif (eregi('win',$Agent) && eregi('nt 5.1',$Agent)) { <br>$os="Windows XP"; <br>} <br>elseif (eregi('win',$Agent) && ereg('32',$Agent)) { <br>$os="Windows 32"; <br>} <br>elseif (eregi('linux',$Agent)) { <br>$os="Linux"; <br>} <br>elseif (eregi('unix',$Agent)) { <br>$os="Unix"; <br>} <br>elseif (eregi('sun',$Agent) && eregi('os',$Agent)) { <br>$os="SunOS"; <br>} <br>elseif (eregi('ibm',$Agent) && eregi('os',$Agent)) { <br>$os="IBM OS/2"; <br>} <br>elseif (eregi('Mac',$Agent) && eregi('PC',$Agent)) { <br>$os="Macintosh"; <br>} <br>elseif (eregi('PowerPC',$Agent)) { <br>$os="PowerPC"; <br>} <br>elseif (eregi('AIX',$Agent)) { <br>$os="AIX"; <br>} <br>elseif (eregi('HPUX',$Agent)) { <br>$os="HPUX"; <br>} <br>elseif (eregi('NetBSD',$Agent)) { <br>$os="NetBSD"; <br>} <br>elseif (eregi('BSD',$Agent)) { <br>$os="BSD"; <br>} <br>elseif (ereg('OSF1',$Agent)) { <br>$os="OSF1"; <br>} <br>elseif (ereg('IRIX',$Agent)) { <br>$os="IRIX"; <br>} <br>elseif (eregi('FreeBSD',$Agent)) { <br>$os="FreeBSD"; <br>} <br>if ($os=='') $os = "Unknown"; <br>return $os; <br>} <br>//调用方法$os=os_infor() ; <br>?> <br> <br>文件格式类 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>$mime_types = array( <br>'gif' => 'image/gif', <br>'jpg' => 'image/jpeg', <br>'jpeg' => 'image/jpeg', <br>'jpe' => 'image/jpeg', <br>'bmp' => 'image/bmp', <br>'png' => 'image/png', <br>'tif' => 'image/tiff', <br>'tiff' => 'image/tiff', <br>'pict' => 'image/x-pict', <br>'pic' => 'image/x-pict', <br>'pct' => 'image/x-pict', <br>'tif' => 'image/tiff', <br>'tiff' => 'image/tiff', <br>'psd' => 'image/x-photoshop', <br>'swf' => 'application/x-shockwave-flash', <br>'js' => 'application/x-javascrīpt', <br>'pdf' => 'application/pdf', <br>'ps' => 'application/postscrīpt', <br>'eps' => 'application/postscrīpt', <br>'ai' => 'application/postscrīpt', <br>'wmf' => 'application/x-msmetafile', <br>'css' => 'text/css', <br>'htm' => 'text/html', <br>'html' => 'text/html', <br>'txt' => 'text/plain', <br>'xml' => 'text/xml', <br>'wml' => 'text/wml', <br>'wbmp' => 'image/vnd.wap.wbmp', <br>'mid' => 'audio/midi', <br>'wav' => 'audio/wav', <br>'mp3' => 'audio/mpeg', <br>'mp2' => 'audio/mpeg', <br>'avi' => 'video/x-msvideo', <br>'mpeg' => 'video/mpeg', <br>'mpg' => 'video/mpeg', <br>'qt' => 'video/quicktime', <br>'mov' => 'video/quicktime', <br>'lha' => 'application/x-lha', <br>'lzh' => 'application/x-lha', <br>'z' => 'application/x-compress', <br>'gtar' => 'application/x-gtar', <br>'gz' => 'application/x-gzip', <br>'gzip' => 'application/x-gzip', <br>'tgz' => 'application/x-gzip', <br>'tar' => 'application/x-tar', <br>'bz2' => 'application/bzip2', <br>'zip' => 'application/zip', <br>'arj' => 'application/x-arj', <br>'rar' => 'application/x-rar-compressed', <br>'hqx' => 'application/mac-binhex40', <br>'sit' => 'application/x-stuffit', <br>'bin' => 'application/x-macbinary', <br>'uu' => 'text/x-uuencode', <br>'uue' => 'text/x-uuencode', <br>'latex'=> 'application/x-latex', <br>'ltx' => 'application/x-latex', <br>'tcl' => 'application/x-tcl', <br>'pgp' => 'application/pgp', <br>'asc' => 'application/pgp', <br>'exe' => 'application/x-msdownload', <br>'doc' => 'application/msword', <br>'rtf' => 'application/rtf', <br>'xls' => 'application/vnd.ms-excel', <br>'ppt' => 'application/vnd.ms-powerpoint', <br>'mdb' => 'application/x-msaccess', <br>'wri' => 'application/x-mswrite', <br>); <br>?> <br> <br>php生成excel文档 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>header("Content-type:application/vnd.ms-excel"); <br>header("Content-Disposition:filename=test.xls"); <br>echo "test1t"; <br>echo "test2tn"; <br>echo "test1t"; <br>echo "test2tn"; <br>echo "test1t"; <br>echo "test2tn"; <br>echo "test1t"; <br>echo "test2tn"; <br>echo "test1t"; <br>echo "test2tn"; <br>echo "test1t"; <br>echo "test2tn"; <br>//改动相应文件头就可以</li></ol></pre>输出.doc .xls等文件格式了 <br>?> <br> <br>时间比较问题 <br>举一个简单例子说明:比如一个论坛对当天发表的贴子用new图片标记一下。 <br>方法一: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>//$db->rows[$i][date]中为数据库中datetime字段值. <br>$today=time(); <br>$theDay=date("Y-m-d H:i:s",$today-24*3600); <br>$newTag=$db->rows[$i][date]>=$theDay?"<img src='../image/newinfor.gif'>":""; <br>//方法二: <br>$newTag=$db->rows[$i][date]>=date("Y-m-d 00:00:00")?"<img src='../image/newinfor.gif'>":""; <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//提取页面和浏览器提交的变量,作用相当于使PHP.INI开了全局变量 <br><? <br>@extract($_SERVER, EXTR_SKIP); <br>@extract($_SESSION, EXTR_SKIP); <br>@extract($_POST, EXTR_SKIP); <br>@extract($_FILES, EXTR_SKIP); <br>@extract($_GET, EXTR_SKIP); <br>@extract($_ENV, EXTR_SKIP); <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//读取文件函数 <br><? <br>function readfromfile($file_name) { <br>if (file_exists($file_name)) { <br>$filenum=fopen($file_name,"r"); <br>flock($filenum,LOCK_EX); <br>$file_data=fread($filenum, filesize($file_name)); <br>rewind($filenum); <br>fclose($filenum); <br>return $file_data; <br>} <br>} <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//写入文件函数 <br><? <br>function writetofile($file_name,$data,$method="w") { <br>$filenum=fopen($file_name,$method); <br>flock($filenum,LOCK_EX); <br>$file_data=fwrite($filenum,$data); <br>fclose($filenum); <br>return $file_data; <br>} <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//页面快速转向 <br><? <br>function turntopage($url="index.php",$info = "页面转向中...",$second=2){ <br>print "<html>n<head>n<title>页面转向中....</title>n"; <br>print "<meta http-equiv="refresh" content="$second;url=$url">n"; <br>print "<style type="text/css">n<!--n"; <br>print "td { font-family: "Verdana", "Arial";font-size: 12px}n"; <br>print "A {COLOR: #000000; TEXT-DECORATION: none}n"; <br>print "-->n</style>n"; <br>print "</head>n<body>n"; <br>print "<table width="100%" border="0" align="center">n"; <br>print " <tr>n"; <br>print " <td height="200"> </td>n"; <br>print " </tr>n"; <br>print " <tr>n"; <br>print " <td align="center">n"; <br>print " <table width="60%" border="0" cellpadding="8" bgcolor="#AA9FFF">n"; <br>print " <tr>n"; <br>print " <td height="30" align="center">页面转向提示信息</td>n"; <br>print " </tr>n"; <br>print " <tr>n"; <br>print " <td align="center">$info</td>n"; <br>print " </tr>n"; <br>print " <tr>n"; <br>print " <td align="center">n"; <br>print " <a href="$url">如果你的浏览器不支持自动跳转,请按这里</a></td>n"; <br>print " </tr>n"; <br>print " </tr>n"; <br>print " </table></td>n"; <br>print " </tr>n"; <br>print " <tr>n"; <br>print " <td height="200"> </td>n"; <br>print " </tr>n"; <br>print "</table>n"; <br>print "</body>n</html>"; <br>exit; <br>?> <br> <br>产生随机字符串函数 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>function random($length) { <br>$hash = @#@#; <br>$chars = @#ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz@#; <br>$max = strlen($chars) - 1; <br>mt_srand((double)microtime() * 1000000); <br>for($i = 0; $i < $length; $i++) { <br>$hash .= $chars[mt_rand(0, $max)]; <br>} <br>return $hash; <br>} <br>?> <br> <br>截取一定长度的字符串(该函数对GB2312使用有效) <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>function Wordscut($string, $length ,$sss=0) { <br>if(strlen($string) > $length) { <br>if($sss){ <br>$length=$length - 3; <br>$addstr=@# ...@#; <br>} <br>for($i = 0; $i < $length; $i++) { <br>if(ord($string[$i]) > 127) { <br>$wordscut .= $string[$i].$string[$i + 1]; <br>$i++; <br>} else { <br>$wordscut .= $string[$i]; <br>} <br>} <br>return $wordscut.$addstr; <br>} <br>return $string; <br>} <br>?> <br> <br>取得客户端IP地址 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>function GetIP(){ <br>if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) <br>$ip = getenv("HTTP_CLIENT_IP"); <br>else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) <br>$ip = getenv("HTTP_X_FORWARDED_FOR"); <br>else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) <br>$ip = getenv("REMOTE_ADDR"); <br>else if (isset($_SERVER[@#REMOTE_ADDR@#]) && $_SERVER[@#REMOTE_ADDR@#] && strcasecmp($_SERVER[@#REMOTE_ADDR@#], "unknown")) <br>$ip = $_SERVER[@#REMOTE_ADDR@#]; <br>else <br>$ip = "unknown"; <br>return($ip); <br>} <br>?> <br> <br>判断邮箱地址 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>function checkEmail($inAddress) <br>{ <br>return (ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+",$inAddress)); <br>} <br>?> <br> <br>分页(两个函数配合使用) <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>function getpage($sql,$page_size=20) <br>{ <br>global $page,$totalpage,$sums; //out param <br>$page = $_GET["page"]; <br>//$eachpage = $page_size; <br>$pagesql = strstr($sql," from "); <br>$pagesql = "select count(*) as ids ".$pagesql; <br>$result = mysql_query($pagesql); <br>if($rs = mysql_fetch_array($result)) $sums = $rs[0]; <br>$totalpage = ceil($sums/$page_size); <br>if((!$page)($page<1)) $page=1; <br>$startpos = ($page-1)*$page_size; <br>$sql .=" limit $startpos,$page_size "; <br>return $sql; <br>} <br>function showbar($string="") <br>{ <br>global $page,$totalpage; <br>$out="共<font ".$totalpage."color=@#red@#><b>".$totalpage."</b></font>页 "; <br>$linkNum =4; <br>$start = ($page-round($linkNum/2))>0 ? ($page-round($linkNum/2)) : "1"; <br>$end = ($page+round($linkNum/2))<$totalpage ? ($page+round($linkNum/2)) : $totalpage; <br>$prestart=$start-1; <br>$nextend=$end+1; <br>if($page<>1) <br>$out .= "<a href=@#?page=1&&".$string."@#title=第一页>第一页</a> "; <br>if($start>1) <br>$out.="<a href=@#?page=".$prestart."@# title=上一页>..<<</a> "; <br>for($t=$start;$t<=$end;$t++) <br>{ <br>$out .= ($page==$t) ? "<font [".$t."]color=@#red@#><b>[".$t."]</b></font> " : "<a $thref=@#?page=$t&&".$string."@#>$t</a> "; <br>} <br>if($end<$totalpage) <br>$out.="<a href=@#?page=".$nextend."&&".$string."@# title=下一页>>>..</a>"; <br>if($page<>$totalpage) <br>$out .= " <a href=@#?page=".$totalpage."&&".$string."@# title=最后页>最后页</a>"; <br>return $out; <br>} <br>?> <br> <br>获取新插入数据的ID <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><? <br>mysql_insert_id(); <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//获得当前的脚本网址 <br><? <br>function get_php_url(){ <br>if(!empty($_server["REQUEST_URI"])){ <br>$scriptName = $_SERVER["REQUEST_URI"]; <br>$nowurl = $scriptName; <br>}else{ <br>$scriptName = $_SERVER["PHP_SELF"]; <br>if(empty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName; <br>else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"]; <br>} <br>return $nowurl; <br>} <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//把全角数字转为半角数字 <br><? <br>function GetAlabNum($fnum){ <br>$nums = array("0","1","2","3","4","5","6","7","8","9"); <br>$fnums = "0123456789"; <br>for($i=0;$i<=9;$i++) $fnum = str_replace($nums[$i],$fnums[$i],$fnum); <br>$fnum = ereg_replace("[^0-9.]|^0{1,}","",$fnum); <br>if($fnum=="") $fnum=0; <br>return $fnum; <br>} <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//去除HTML标记 <br><? <br>function Text2Html($txt){ <br>$txt = str_replace(" "," ",$txt); <br>$txt = str_replace("<","<",$txt); <br>$txt = str_replace(">",">",$txt); <br>$txt = preg_replace("/[rn]{1,}/isU"," <br>rn",$txt); <br>return $txt; <br>} <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//相对路径转化成绝对路径 <br><? <br>function relative_to_absolute($content, $feed_url) { <br>preg_match('/(http|https|ftp):///', $feed_url, $protocol); <br>$server_url = preg_replace("/(http|https|ftp|news):///", "", $feed_url); <br>$server_url = preg_replace("//.*/", "", $server_url); <br>if ($server_url == '') { <br>return $content; <br>} <br>if (isset($protocol[0])) { <br>$new_content = preg_replace('/href="//', 'href="'.$protocol[0].$server_url.'/', $content); <br>$new_content = preg_replace('/src="//', 'src="'.$protocol[0].$server_url.'/', $new_content); <br>} else { <br>$new_content = $content; <br>} <br>return $new_content; <br>} <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//取得所有链接 <br><? <br>function get_all_url($code){ <br>preg_match_all('/<as+href=["|']?([^>"' ]+)["|']?s*[^>]*>([^>]+)</a>/i',$code,$arr); <br>return array('name'=>$arr[2],'url'=>$arr[1]); <br>} <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//HTML表格的每行转为CSV格式数组 <br><? <br>function get_tr_array($table) { <br>$table = preg_replace("'<td[^>]*?>'si",'"',$table); <br>$table = str_replace("</td>",'",',$table); <br>$table = str_replace("</tr>","{tr}",$table); <br>//去掉 HTML 标记 <br>$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table); <br>//去掉空白字符 <br>$table = preg_replace("'([rn])[s]+'","",$table); <br>$table = str_replace(" ","",$table); <br>$table = str_replace(" ","",$table); <br>$table = explode(",{tr}",$table); <br>array_pop($table); <br>return $table; <br>} <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//将HTML表格的每行每列转为数组,采集表格数据 <br><? <br>function get_td_array($table) { <br>$table = preg_replace("'<table[^>]*?>'si","",$table); <br>$table = preg_replace("'<tr[^>]*?>'si","",$table); <br>$table = preg_replace("'<td[^>]*?>'si","",$table); <br>$table = str_replace("</tr>","{tr}",$table); <br>$table = str_replace("</td>","{td}",$table); <br>//去掉 HTML 标记 <br>$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table); <br>//去掉空白字符 <br>$table = preg_replace("'([rn])[s]+'","",$table); <br>$table = str_replace(" ","",$table); <br>$table = str_replace(" ","",$table); <br>$table = explode('{tr}', $table); <br>array_pop($table); <br>foreach ($table as $key=>$tr) { <br>$td = explode('{td}', $tr); <br>array_pop($td); <br>$td_array[] = $td; <br>} <br>return $td_array; <br>} <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//返回字符串中的所有单词 $distinct=true 去除重复 <br><? <br>function split_en_str($str,$distinct=true) { <br>preg_match_all('/([a-zA-Z]+)/',$str,$match); <br>if ($distinct == true) { <br>$match[1] = array_unique($match[1]); <br>} <br>sort($match[1]); <br>return $match[1]; <br>} <br>?> <br> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//打印出为本PHP项目做出贡献的人员的清单 <br><? <br>string phpcredits(void) <br>?> <br> <br></li><li> </li><li> </li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre>

人气教程排行