Rumah >pembangunan bahagian belakang >tutorial php >php 的常用函数FUNCTION

php 的常用函数FUNCTION

WBOY
WBOYasal
2016-07-28 08:28:56866semak imbas
<span></span><span>php
</span><span>/**
 * 获取客户端IP
 * @return [string] [description]
 */</span><span>
functiongetClientIp</span><span>()</span><span>{</span><span>
    $ip</span><span>=</span><span> NULL</span><span>;</span><span>if</span><span>(</span><span>isset</span><span>(</span><span>$_SERVER</span><span>[</span><span>'HTTP_X_FORWARDED_FOR'</span><span>]))</span><span>{</span><span>
        $arr</span><span>=</span><span>explode</span><span>(</span><span>','</span><span>,</span><span>$_SERVER</span><span>[</span><span>'HTTP_X_FORWARDED_FOR'</span><span>]);</span><span>
        $pos</span><span>=</span><span> array_search</span><span>(</span><span>'unknown'</span><span>,</span><span>$arr</span><span>);</span><span>if</span><span>(</span><span>false</span><span>!==</span><span>$pos</span><span>)</span><span> unset</span><span>(</span><span>$arr</span><span>[</span><span>$pos</span><span>]);</span><span>
        $ip  </span><span>=</span><span>  trim</span><span>(</span><span>$arr</span><span>[</span><span>0</span><span>]);</span><span>}</span><span>elseif</span><span>(</span><span>isset</span><span>(</span><span>$_SERVER</span><span>[</span><span>'HTTP_CLIENT_IP'</span><span>]))</span><span>{</span><span>
        $ip</span><span>=</span><span>$_SERVER</span><span>[</span><span>'HTTP_CLIENT_IP'</span><span>];</span><span>}</span><span>elseif</span><span>(</span><span>isset</span><span>(</span><span>$_SERVER</span><span>[</span><span>'REMOTE_ADDR'</span><span>]))</span><span>{</span><span>
        $ip</span><span>=</span><span>$_SERVER</span><span>[</span><span>'REMOTE_ADDR'</span><span>];</span><span>}</span><span>// IP地址合法验证</span><span>
    $ip</span><span>=</span><span>(</span><span>false</span><span>!==</span><span>ip2long</span><span>(</span><span>$ip</span><span>))</span><span>?</span><span>$ip</span><span>:</span><span>'0.0.0.0'</span><span>;</span><span>return</span><span>$ip</span><span>;</span><span>}</span><span>/**
 * 获取在线IP
 * @return String
 */</span><span>
functiongetOnlineIp</span><span>(</span><span>$format</span><span>=</span><span>0</span><span>)</span><span>{</span><span>global</span><span>$S_GLOBAL</span><span>;</span><span>if</span><span>(</span><span>empty</span><span>(</span><span>$S_GLOBAL</span><span>[</span><span>'onlineip'</span><span>]))</span><span>{</span><span>if</span><span>(</span><span>getenv</span><span>(</span><span>'HTTP_CLIENT_IP'</span><span>)</span><span>&&</span><span>strcasecmp</span><span>(</span><span>getenv</span><span>(</span><span>'HTTP_CLIENT_IP'</span><span>),</span><span>'unknown'</span><span>))</span><span>{</span><span>
            $onlineip</span><span>=</span><span>getenv</span><span>(</span><span>'HTTP_CLIENT_IP'</span><span>);</span><span>}</span><span>elseif</span><span>(</span><span>getenv</span><span>(</span><span>'HTTP_X_FORWARDED_FOR'</span><span>)</span><span>&&</span><span>strcasecmp</span><span>(</span><span>getenv</span><span>(</span><span>'HTTP_X_FORWARDED_FOR'</span><span>),</span><span>'unknown'</span><span>))</span><span>{</span><span>
            $onlineip</span><span>=</span><span>getenv</span><span>(</span><span>'HTTP_X_FORWARDED_FOR'</span><span>);</span><span>}</span><span>elseif</span><span>(</span><span>getenv</span><span>(</span><span>'REMOTE_ADDR'</span><span>)</span><span>&&</span><span>strcasecmp</span><span>(</span><span>getenv</span><span>(</span><span>'REMOTE_ADDR'</span><span>),</span><span>'unknown'</span><span>))</span><span>{</span><span>
            $onlineip</span><span>=</span><span>getenv</span><span>(</span><span>'REMOTE_ADDR'</span><span>);</span><span>}</span><span>elseif</span><span>(</span><span>isset</span><span>(</span><span>$_SERVER</span><span>[</span><span>'REMOTE_ADDR'</span><span>])</span><span>&&</span><span>$_SERVER</span><span>[</span><span>'REMOTE_ADDR'</span><span>]</span><span>&&</span><span>strcasecmp</span><span>(</span><span>$_SERVER</span><span>[</span><span>'REMOTE_ADDR'</span><span>],</span><span>'unknown'</span><span>))</span><span>{</span><span>
            $onlineip</span><span>=</span><span>$_SERVER</span><span>[</span><span>'REMOTE_ADDR'</span><span>];</span><span>}</span><span>
        preg_match</span><span>(</span><span>"/[\d\.]{7,15}/"</span><span>,</span><span>$onlineip</span><span>,</span><span>$onlineipmatches</span><span>);</span><span>
        $S_GLOBAL</span><span>[</span><span>'onlineip'</span><span>]</span><span>=</span><span>$onlineipmatches</span><span>[</span><span>0</span><span>]</span><span>?</span><span>$onlineipmatches</span><span>[</span><span>0</span><span>]</span><span>:</span><span>'unknown'</span><span>;</span><span>}</span><span>if</span><span>(</span><span>$format</span><span>)</span><span>{</span><span>
        $ips</span><span>=</span><span>explode</span><span>(</span><span>'.'</span><span>,</span><span>$S_GLOBAL</span><span>[</span><span>'onlineip'</span><span>]);</span><span>for</span><span>(</span><span>$i</span><span>=</span><span>0</span><span>;</span><span>$i</span><span><span>3</span><span>;</span><span>$i</span><span>++)</span><span>{</span><span>
            $ips</span><span>[</span><span>$i</span><span>]</span><span>=</span><span>intval</span><span>(</span><span>$ips</span><span>[</span><span>$i</span><span>]);</span><span>}</span><span>
        returnsprintf</span><span>(</span><span>'%03d%03d%03d'</span><span>,</span><span>$ips</span><span>[</span><span>0</span><span>],</span><span>$ips</span><span>[</span><span>1</span><span>],</span><span>$ips</span><span>[</span><span>2</span><span>]);</span><span>}</span><span>else</span><span>{</span><span>return</span><span>$S_GLOBAL</span><span>[</span><span>'onlineip'</span><span>];</span><span>}</span><span>}</span><span>/**
 * 获取url
 * @return [type] [description]
 */</span><span>
functiongetUrl</span><span>(){</span><span>
  $pageURL</span><span>=</span><span>'http'</span><span>;</span><span>if</span><span>(</span><span>isset</span><span>(</span><span>$_SERVER</span><span>[</span><span>"HTTPS"</span><span>])</span><span>&&</span><span>$_SERVER</span><span>[</span><span>"HTTPS"</span><span>]</span><span>==</span><span>"on"</span><span>)</span><span>{</span><span>
    $pageURL</span><span>.=</span><span>"s"</span><span>;</span><span>}</span><span>
  $pageURL</span><span>.=</span><span>"://"</span><span>;</span><span>if</span><span>(</span><span>$_SERVER</span><span>[</span><span>"SERVER_PORT"</span><span>]</span><span>!=</span><span>"80"</span><span>)</span><span>{</span><span>
    $pageURL</span><span>.=</span><span>$_SERVER</span><span>[</span><span>"HTTP_HOST"</span><span>]</span><span>.</span><span>":"</span><span>.</span><span>$_SERVER</span><span>[</span><span>"SERVER_PORT"</span><span>]</span><span>.</span><span>$_SERVER</span><span>[</span><span>"REQUEST_URI"</span><span>];</span><span>}</span><span>else</span><span>{</span><span>
    $pageURL</span><span>.=</span><span>$_SERVER</span><span>[</span><span>"HTTP_HOST"</span><span>]</span><span>.</span><span>$_SERVER</span><span>[</span><span>"REQUEST_URI"</span><span>];</span><span>}</span><span>return</span><span>$pageURL</span><span>;</span><span>}</span><span>/**
 * 获取当前站点的访问路径根目录
 * @return [type] [description]
 */</span><span>
functiongetSiteUrl</span><span>()</span><span>{</span><span>
    $uri</span><span>=</span><span>$_SERVER</span><span>[</span><span>'REQUEST_URI'</span><span>]?</span><span>$_SERVER</span><span>[</span><span>'REQUEST_URI'</span><span>]:(</span><span>$_SERVER</span><span>[</span><span>'PHP_SELF'</span><span>]?</span><span>$_SERVER</span><span>[</span><span>'PHP_SELF'</span><span>]:</span><span>$_SERVER</span><span>[</span><span>'SCRIPT_NAME'</span><span>]);</span><span>return</span><span>'http://'</span><span>.</span><span>$_SERVER</span><span>[</span><span>'HTTP_HOST'</span><span>].</span><span>substr</span><span>(</span><span>$uri</span><span>,</span><span>0</span><span>,</span><span>strrpos</span><span>(</span><span>$uri</span><span>,</span><span>'/'</span><span>)+</span><span>1</span><span>);</span><span>}</span><span>/**
 * 字符串截取,支持中文和其他编码
 * @param  [string]  $str     [字符串]
 * @param  integer $start   [起始位置]
 * @param  integer $length  [截取长度]
 * @param  string  $charset [字符串编码]
 * @param  boolean $suffix  [是否有省略号]
 * @return [type]           [description]
 */</span><span>
functionmsubstr</span><span>(</span><span>$str</span><span>,</span><span>$start</span><span>=</span><span>0</span><span>,</span><span>$length</span><span>=</span><span>15</span><span>,</span><span>$charset</span><span>=</span><span>"utf-8"</span><span>,</span><span>$suffix</span><span>=</span><span>true</span><span>)</span><span>{</span><span>if</span><span>(</span><span>function_exists</span><span>(</span><span>"mb_substr"</span><span>))</span><span>{</span><span>
        returnmb_substr</span><span>(</span><span>$str</span><span>,</span><span>$start</span><span>,</span><span>$length</span><span>,</span><span>$charset</span><span>);</span><span>}</span><span>elseif</span><span>(</span><span>function_exists</span><span>(</span><span>'iconv_substr'</span><span>))</span><span>{</span><span>
        returniconv_substr</span><span>(</span><span>$str</span><span>,</span><span>$start</span><span>,</span><span>$length</span><span>,</span><span>$charset</span><span>);</span><span>}</span><span>
    $re</span><span>[</span><span>'utf-8'</span><span>]</span><span>=</span><span>"/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"</span><span>;</span><span>
    $re</span><span>[</span><span>'gb2312'</span><span>]</span><span>=</span><span>"/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"</span><span>;</span><span>
    $re</span><span>[</span><span>'gbk'</span><span>]</span><span>=</span><span>"/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"</span><span>;</span><span>
    $re</span><span>[</span><span>'big5'</span><span>]</span><span>=</span><span>"/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"</span><span>;</span><span>
    preg_match_all</span><span>(</span><span>$re</span><span>[</span><span>$charset</span><span>],</span><span>$str</span><span>,</span><span>$match</span><span>);</span><span>
    $slice</span><span>=</span><span> join</span><span>(</span><span>""</span><span>,</span><span>array_slice</span><span>(</span><span>$match</span><span>[</span><span>0</span><span>],</span><span>$start</span><span>,</span><span>$length</span><span>));</span><span>if</span><span>(</span><span>$suffix</span><span>)</span><span>{</span><span>return</span><span>$slice</span><span>.</span><span>"…"</span><span>;</span><span>}</span><span>return</span><span>$slice</span><span>;</span><span>}</span><span>/**
 * php 实现js escape 函数
 * @param  [type] $string   [description]
 * @param  string $encoding [description]
 * @return [type]           [description]
 */</span><span>
functionescape</span><span>(</span><span>$string</span><span>,</span><span>$encoding</span><span>=</span><span>'UTF-8'</span><span>){</span><span>
  $return</span><span>=</span><span>null</span><span>;</span><span>for</span><span>(</span><span>$x</span><span>=</span><span>0</span><span>;</span><span>$x</span><span><span> mb_strlen</span><span>(</span><span>$string</span><span>,</span><span>$encoding</span><span>);</span><span>$x</span><span>++)</span><span>{</span><span>
    $str</span><span>=</span><span> mb_substr</span><span>(</span><span>$string</span><span>,</span><span>$x</span><span>,</span><span>1</span><span>,</span><span>$encoding</span><span>);</span><span>if</span><span>(</span><span>strlen</span><span>(</span><span>$str</span><span>)</span><span>></span><span>1</span><span>)</span><span>{</span><span>// 多字节字符</span><span>
      $return</span><span>.=</span><span>"%u"</span><span>.</span><span>strtoupper</span><span>(</span><span>bin2hex</span><span>(</span><span>mb_convert_encoding</span><span>(</span><span>$str</span><span>,</span><span>'UCS-2'</span><span>,</span><span>$encoding</span><span>)));</span><span>}</span><span>else</span><span>{</span><span>
      $return</span><span>.=</span><span>"%"</span><span>.</span><span>strtoupper</span><span>(</span><span>bin2hex</span><span>(</span><span>$str</span><span>));</span><span>}</span><span>}</span><span>return</span><span>$return</span><span>;</span><span>}</span><span>/**
 * php 实现 js unescape函数
 * @param  [type] $str [description]
 * @return [type]      [description]
 */</span><span>
functionunescape</span><span>(</span><span>$str</span><span>)</span><span>{</span><span>
    $str</span><span>=</span><span> rawurldecode</span><span>(</span><span>$str</span><span>);</span><span>
    preg_match_all</span><span>(</span><span>"/(?:%u.{4})|.{4};|\d+;|.+/U"</span><span>,</span><span>$str</span><span>,</span><span>$r</span><span>);</span><span>
    $ar</span><span>=</span><span>$r</span><span>[</span><span>0</span><span>];</span><span>foreach</span><span>(</span><span>$aras$k</span><span>=></span><span>$v</span><span>)</span><span>{</span><span>if</span><span>(</span><span>substr</span><span>(</span><span>$v</span><span>,</span><span>0</span><span>,</span><span>2</span><span>)</span><span>==</span><span>"%u"</span><span>){</span><span>
            $ar</span><span>[</span><span>$k</span><span>]</span><span>=</span><span> iconv</span><span>(</span><span>"UCS-2"</span><span>,</span><span>"utf-8//IGNORE"</span><span>,</span><span>pack</span><span>(</span><span>"H4"</span><span>,</span><span>substr</span><span>(</span><span>$v</span><span>,-</span><span>4</span><span>)));</span><span>}</span><span>elseif</span><span>(</span><span>substr</span><span>(</span><span>$v</span><span>,</span><span>0</span><span>,</span><span>3</span><span>)</span><span>==</span><span>""</span><span>)</span><span>{</span><span>
            $ar</span><span>[</span><span>$k</span><span>]</span><span>=</span><span> iconv</span><span>(</span><span>"UCS-2"</span><span>,</span><span>"utf-8"</span><span>,</span><span>pack</span><span>(</span><span>"H4"</span><span>,</span><span>substr</span><span>(</span><span>$v</span><span>,</span><span>3</span><span>,-</span><span>1</span><span>)));</span><span>}</span><span>elseif</span><span>(</span><span>substr</span><span>(</span><span>$v</span><span>,</span><span>0</span><span>,</span><span>2</span><span>)</span><span>==</span><span>""</span><span>)</span><span>{</span><span>
            echosubstr</span><span>(</span><span>$v</span><span>,</span><span>2</span><span>,-</span><span>1</span><span>).</span><span>""</span><span>;</span><span>
            $ar</span><span>[</span><span>$k</span><span>]</span><span>=</span><span> iconv</span><span>(</span><span>"UCS-2"</span><span>,</span><span>"utf-8"</span><span>,</span><span>pack</span><span>(</span><span>"n"</span><span>,</span><span>substr</span><span>(</span><span>$v</span><span>,</span><span>2</span><span>,-</span><span>1</span><span>)));</span><span>}</span><span>}</span><span>
    returnjoin</span><span>(</span><span>""</span><span>,</span><span>$ar</span><span>);</span><span>}</span><span>/**
 * 数字转人名币
 * @param  [type] $num [description]
 * @return [type]      [description]
 */</span><span>
functionnum2rmb </span><span>(</span><span>$num</span><span>)</span><span>{</span><span>
    $c1</span><span>=</span><span>"零壹贰叁肆伍陆柒捌玖"</span><span>;</span><span>
    $c2</span><span>=</span><span>"分角元拾佰仟万拾佰仟亿"</span><span>;</span><span>
    $num</span><span>=</span><span>round</span><span>(</span><span>$num</span><span>,</span><span>2</span><span>);</span><span>
    $num</span><span>=</span><span>$num</span><span>*</span><span>100</span><span>;</span><span>if</span><span>(</span><span>strlen</span><span>(</span><span>$num</span><span>)</span><span>></span><span>10</span><span>)</span><span>{</span><span>return</span><span>"oh,sorry,the number is too long!"</span><span>;</span><span>}</span><span>
    $i</span><span>=</span><span>0</span><span>;</span><span>
    $c</span><span>=</span><span>""</span><span>;</span><span>while</span><span>(</span><span>1</span><span>)</span><span>{</span><span>if</span><span>(</span><span>$i</span><span>==</span><span>0</span><span>)</span><span>{</span><span>
            $n</span><span>=</span><span>substr</span><span>(</span><span>$num</span><span>,</span><span>strlen</span><span>(</span><span>$num</span><span>)-</span><span>1</span><span>,</span><span>1</span><span>);</span><span>}</span><span>else</span><span>{</span><span>
            $n</span><span>=</span><span>$num</span><span>%</span><span>10</span><span>;</span><span>}</span><span>
        $p1</span><span>=</span><span>substr</span><span>(</span><span>$c1</span><span>,</span><span>3</span><span>*</span><span>$n</span><span>,</span><span>3</span><span>);</span><span>
        $p2</span><span>=</span><span>substr</span><span>(</span><span>$c2</span><span>,</span><span>3</span><span>*</span><span>$i</span><span>,</span><span>3</span><span>);</span><span>if</span><span>(</span><span>$n</span><span>!=</span><span>'0'</span><span>||</span><span>(</span><span>$n</span><span>==</span><span>'0'</span><span>&&</span><span>(</span><span>$p2</span><span>==</span><span>'亿'</span><span>||</span><span>$p2</span><span>==</span><span>'万'</span><span>||</span><span>$p2</span><span>==</span><span>'元'</span><span>)))</span><span>{</span><span>
            $c</span><span>=</span><span>$p1</span><span>.</span><span>$p2</span><span>.</span><span>$c</span><span>;</span><span>}</span><span>else</span><span>{</span><span>
            $c</span><span>=</span><span>$p1</span><span>.</span><span>$c</span><span>;</span><span>}</span><span>
        $i</span><span>=</span><span>$i</span><span>+</span><span>1</span><span>;</span><span>
        $num</span><span>=</span><span>$num</span><span>/</span><span>10</span><span>;</span><span>
        $num</span><span>=</span><span>(</span><span>int</span><span>)</span><span>$num</span><span>;</span><span>if</span><span>(</span><span>$num</span><span>==</span><span>0</span><span>)</span><span>{</span><span>break</span><span>;</span><span>}</span><span>}</span><span>
    $j</span><span>=</span><span>0</span><span>;</span><span>
    $slen</span><span>=</span><span>strlen</span><span>(</span><span>$c</span><span>);</span><span>while</span><span>(</span><span>$j</span><span><span>$slen</span><span>)</span><span>{</span><span>
        $m</span><span>=</span><span>substr</span><span>(</span><span>$c</span><span>,</span><span>$j</span><span>,</span><span>6</span><span>);</span><span>if</span><span>(</span><span>$m</span><span>==</span><span>'零元'</span><span>||</span><span>$m</span><span>==</span><span>'零万'</span><span>||</span><span>$m</span><span>==</span><span>'零亿'</span><span>||</span><span>$m</span><span>==</span><span>'零零'</span><span>)</span><span>{</span><span>
            $left</span><span>=</span><span>substr</span><span>(</span><span>$c</span><span>,</span><span>0</span><span>,</span><span>$j</span><span>);</span><span>
            $right</span><span>=</span><span>substr</span><span>(</span><span>$c</span><span>,</span><span>$j</span><span>+</span><span>3</span><span>);</span><span>
            $c</span><span>=</span><span>$left</span><span>.</span><span>$right</span><span>;</span><span>
            $j</span><span>=</span><span>$j</span><span>-</span><span>3</span><span>;</span><span>
            $slen</span><span>=</span><span>$slen</span><span>-</span><span>3</span><span>;</span><span>}</span><span>
        $j</span><span>=</span><span>$j</span><span>+</span><span>3</span><span>;</span><span>}</span><span>if</span><span>(</span><span>substr</span><span>(</span><span>$c</span><span>,</span><span>strlen</span><span>(</span><span>$c</span><span>)-</span><span>3</span><span>,</span><span>3</span><span>)</span><span>==</span><span>'零'</span><span>)</span><span>{</span><span>
        $c</span><span>=</span><span>substr</span><span>(</span><span>$c</span><span>,</span><span>0</span><span>,</span><span>strlen</span><span>(</span><span>$c</span><span>)-</span><span>3</span><span>);</span><span>}</span><span>// if there is a '0' on the end , chop it out</span><span>return</span><span>$c</span><span>.</span><span>"整"</span><span>;</span><span>}</span><span>/**
 * 特殊的字符
 * @param  [type] $str [description]
 * @return [type]      [description]
 */</span><span>
functionmakeSemiangle</span><span>(</span><span>$str</span><span>)</span><span>{</span><span>
  $arr</span><span>=</span><span>array</span><span>(</span><span>'0'</span><span>=></span><span>'0'</span><span>,</span><span>'1'</span><span>=></span><span>'1'</span><span>,</span><span>'2'</span><span>=></span><span>'2'</span><span>,</span><span>'3'</span><span>=></span><span>'3'</span><span>,</span><span>'4'</span><span>=></span><span>'4'</span><span>,</span><span>'5'</span><span>=></span><span>'5'</span><span>,</span><span>'6'</span><span>=></span><span>'6'</span><span>,</span><span>'7'</span><span>=></span><span>'7'</span><span>,</span><span>'8'</span><span>=></span><span>'8'</span><span>,</span><span>'9'</span><span>=></span><span>'9'</span><span>,</span><span>'A'</span><span>=></span><span>'A'</span><span>,</span><span>'B'</span><span>=></span><span>'B'</span><span>,</span><span>'C'</span><span>=></span><span>'C'</span><span>,</span><span>'D'</span><span>=></span><span>'D'</span><span>,</span><span>'E'</span><span>=></span><span>'E'</span><span>,</span><span>'F'</span><span>=></span><span>'F'</span><span>,</span><span>'G'</span><span>=></span><span>'G'</span><span>,</span><span>'H'</span><span>=></span><span>'H'</span><span>,</span><span>'I'</span><span>=></span><span>'I'</span><span>,</span><span>'J'</span><span>=></span><span>'J'</span><span>,</span><span>'K'</span><span>=></span><span>'K'</span><span>,</span><span>'L'</span><span>=></span><span>'L'</span><span>,</span><span>'M'</span><span>=></span><span>'M'</span><span>,</span><span>'N'</span><span>=></span><span>'N'</span><span>,</span><span>'O'</span><span>=></span><span>'O'</span><span>,</span><span>'P'</span><span>=></span><span>'P'</span><span>,</span><span>'Q'</span><span>=></span><span>'Q'</span><span>,</span><span>'R'</span><span>=></span><span>'R'</span><span>,</span><span>'S'</span><span>=></span><span>'S'</span><span>,</span><span>'T'</span><span>=></span><span>'T'</span><span>,</span><span>'U'</span><span>=></span><span>'U'</span><span>,</span><span>'V'</span><span>=></span><span>'V'</span><span>,</span><span>'W'</span><span>=></span><span>'W'</span><span>,</span><span>'X'</span><span>=></span><span>'X'</span><span>,</span><span>'Y'</span><span>=></span><span>'Y'</span><span>,</span><span>'Z'</span><span>=></span><span>'Z'</span><span>,</span><span>'a'</span><span>=></span><span>'a'</span><span>,</span><span>'b'</span><span>=></span><span>'b'</span><span>,</span><span>'c'</span><span>=></span><span>'c'</span><span>,</span><span>'d'</span><span>=></span><span>'d'</span><span>,</span><span>'e'</span><span>=></span><span>'e'</span><span>,</span><span>'f'</span><span>=></span><span>'f'</span><span>,</span><span>'g'</span><span>=></span><span>'g'</span><span>,</span><span>'h'</span><span>=></span><span>'h'</span><span>,</span><span>'i'</span><span>=></span><span>'i'</span><span>,</span><span>'j'</span><span>=></span><span>'j'</span><span>,</span><span>'k'</span><span>=></span><span>'k'</span><span>,</span><span>'l'</span><span>=></span><span>'l'</span><span>,</span><span>'m'</span><span>=></span><span>'m'</span><span>,</span><span>'n'</span><span>=></span><span>'n'</span><span>,</span><span>'o'</span><span>=></span><span>'o'</span><span>,</span><span>'p'</span><span>=></span><span>'p'</span><span>,</span><span>'q'</span><span>=></span><span>'q'</span><span>,</span><span>'r'</span><span>=></span><span>'r'</span><span>,</span><span>'s'</span><span>=></span><span>'s'</span><span>,</span><span>'t'</span><span>=></span><span>'t'</span><span>,</span><span>'u'</span><span>=></span><span>'u'</span><span>,</span><span>'v'</span><span>=></span><span>'v'</span><span>,</span><span>'w'</span><span>=></span><span>'w'</span><span>,</span><span>'x'</span><span>=></span><span>'x'</span><span>,</span><span>'y'</span><span>=></span><span>'y'</span><span>,</span><span>'z'</span><span>=></span><span>'z'</span><span>,</span><span>'('</span><span>=></span><span>'('</span><span>,</span><span>')'</span><span>=></span><span>')'</span><span>,</span><span>'〔'</span><span>=></span><span>'['</span><span>,</span><span>'〕'</span><span>=></span><span>']'</span><span>,</span><span>'【'</span><span>=></span><span>'['</span><span>,</span><span>'】'</span><span>=></span><span>']'</span><span>,</span><span>'〖'</span><span>=></span><span>'['</span><span>,</span><span>'〗'</span><span>=></span><span>']'</span><span>,</span><span>'{'</span><span>=></span><span>'{'</span><span>,</span><span>'}'</span><span>=></span><span>'}'</span><span>,</span><span>'《'</span><span>=></span><span>'<span>,</span><span>'》'</span><span>=></span><span>'>'</span><span>,</span><span>'%'</span><span>=></span><span>'%'</span><span>,</span><span>'+'</span><span>=></span><span>'+'</span><span>,</span><span>'—'</span><span>=></span><span>'-'</span><span>,</span><span>'-'</span><span>=></span><span>'-'</span><span>,</span><span>'~'</span><span>=></span><span>'-'</span><span>,</span><span>':'</span><span>=></span><span>':'</span><span>,</span><span>'。'</span><span>=></span><span>'.'</span><span>,</span><span>'、'</span><span>=></span><span>','</span><span>,</span><span>','</span><span>=></span><span>'.'</span><span>,</span><span>'、'</span><span>=></span><span>'.'</span><span>,</span><span>';'</span><span>=></span><span>';'</span><span>,</span><span>'?'</span><span>=></span><span>'?'</span><span>,</span><span>'!'</span><span>=></span><span>'!'</span><span>,</span><span>'…'</span><span>=></span><span>'-'</span><span>,</span><span>'‖'</span><span>=></span><span>'|'</span><span>,</span><span>'”'</span><span>=></span><span>'"'</span><span>,</span><span>'“'</span><span>=></span><span>'"'</span><span>,</span><span>'’'</span><span>=></span><span>'`'</span><span>,</span><span>'‘'</span><span>=></span><span>'`'</span><span>,</span><span>'|'</span><span>=></span><span>'|'</span><span>,</span><span>'〃'</span><span>=></span><span>'"'</span><span>,</span><span>' '</span><span>=></span><span>' '</span><span>,</span><span>'.'</span><span>=></span><span>'.'</span><span>);</span><span>
  returnstrtr</span><span>(</span><span>$str</span><span>,</span><span>$arr</span><span>);</span><span>}</span><span>/**
 * 下载
 * @param  [type] $filename [description]
 * @param  string $dir      [description]
 * @return [type]           [description]
 */</span><span>
functiondownloads</span><span>(</span><span>$filename</span><span>,</span><span>$dir</span><span>=</span><span>'./'</span><span>){</span><span>
    $filepath</span><span>=</span><span>$dir</span><span>.</span><span>$filename</span><span>;</span><span>if</span><span>(!</span><span>file_exists</span><span>(</span><span>$filepath</span><span>)){</span><span>
        header</span><span>(</span><span>"Content-type: text/html; charset=utf-8"</span><span>);</span><span>
        echo</span><span>"File not found!"</span><span>;</span><span>exit</span><span>;</span><span>}</span><span>else</span><span>{</span><span>
        $file</span><span>=</span><span>fopen</span><span>(</span><span>$filepath</span><span>,</span><span>"r"</span><span>);</span><span>Header</span><span>(</span><span>"Content-type: application/octet-stream"</span><span>);</span><span>Header</span><span>(</span><span>"Accept-Ranges: bytes"</span><span>);</span><span>Header</span><span>(</span><span>"Accept-Length: "</span><span>.</span><span>filesize</span><span>(</span><span>$filepath</span><span>));</span><span>Header</span><span>(</span><span>"Content-Disposition: attachment; filename="</span><span>.</span><span>$filename</span><span>);</span><span>
        echofread</span><span>(</span><span>$file</span><span>,</span><span>filesize</span><span>(</span><span>$filepath</span><span>));</span><span>
        fclose</span><span>(</span><span>$file</span><span>);</span><span>}</span><span>}</span><span>/**
 * 创建一个目录树
 * @param  [type]  $dir  [description]
 * @param  integer $mode [description]
 * @return [type]        [description]
 */</span><span>
functionmkdirs</span><span>(</span><span>$dir</span><span>,</span><span>$mode</span><span>=</span><span>0777</span><span>)</span><span>{</span><span>if</span><span>(!</span><span>is_dir</span><span>(</span><span>$dir</span><span>))</span><span>{</span><span>
        mkdirs</span><span>(</span><span>dirname</span><span>(</span><span>$dir</span><span>),</span><span>$mode</span><span>);</span><span>
        returnmkdir</span><span>(</span><span>$dir</span><span>,</span><span>$mode</span><span>);</span><span>}</span><span>
    returntrue</span><span>;</span><span>}</span><span>3.</span><span>[代码]</span><span>curl 
functionxcurl</span><span>(</span><span>$url</span><span>,</span><span>$ref</span><span>=</span><span>null</span><span>,</span><span>$post</span><span>=</span><span>array</span><span>(),</span><span>$ua</span><span>=</span><span>"Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre"</span><span>,</span><span>$print</span><span>=</span><span>false</span><span>)</span><span>{</span><span>
    $ch</span><span>=</span><span> curl_init</span><span>();</span><span>
    curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span> CURLOPT_AUTOREFERER</span><span>,</span><span>true</span><span>);</span><span>if</span><span>(!</span><span>empty</span><span>(</span><span>$ref</span><span>))</span><span>{</span><span>
        curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span> CURLOPT_REFERER</span><span>,</span><span>$ref</span><span>);</span><span>}</span><span>
    curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span> CURLOPT_URL</span><span>,</span><span>$url</span><span>);</span><span>
    curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span> CURLOPT_HEADER</span><span>,</span><span>0</span><span>);</span><span>
    curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span> CURLOPT_FOLLOWLOCATION</span><span>,</span><span>1</span><span>);</span><span>
    curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span> CURLOPT_RETURNTRANSFER</span><span>,</span><span>1</span><span>);</span><span>if</span><span>(!</span><span>empty</span><span>(</span><span>$ua</span><span>))</span><span>{</span><span>
        curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span> CURLOPT_USERAGENT</span><span>,</span><span>$ua</span><span>);</span><span>}</span><span>if</span><span>(</span><span>count</span><span>(</span><span>$post</span><span>)</span><span>></span><span>0</span><span>){</span><span>
        curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span> CURLOPT_POST</span><span>,</span><span>1</span><span>);</span><span>
        curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span> CURLOPT_POSTFIELDS</span><span>,</span><span>$post</span><span>);</span><span>}</span><span>
    $output</span><span>=</span><span> curl_exec</span><span>(</span><span>$ch</span><span>);</span><span>
    curl_close</span><span>(</span><span>$ch</span><span>);</span><span>if</span><span>(</span><span>$print</span><span>)</span><span>{</span><span>print</span><span>(</span><span>$output</span><span>);</span><span>}</span><span>else</span><span>{</span><span>return</span><span>$output</span><span>;</span><span>}</span><span>}</span><span>4.</span><span>[代码]日期时间函数</span><span>/**
 * 根据一个时间戳得到详细信息
 * @param  [type] $time [时间戳]
 * @return [type]      
 * @author [yangsheng@yahoo.com]
 */</span><span>
functiongetDateInfo</span><span>(</span><span>$time</span><span>){</span><span>
    $day_of_week_cn</span><span>=</span><span>array</span><span>(</span><span>"日"</span><span>,</span><span>"一"</span><span>,</span><span>"二"</span><span>,</span><span>"三"</span><span>,</span><span>"四"</span><span>,</span><span>"五"</span><span>,</span><span>"六"</span><span>);</span><span>//中文星期</span><span>
    $week_of_month_cn</span><span>=</span><span>array</span><span>(</span><span>''</span><span>,</span><span>'第1周'</span><span>,</span><span>'第2周'</span><span>,</span><span>'第3周'</span><span>,</span><span>'第4周'</span><span>,</span><span>'第5周'</span><span>,</span><span>'第6周'</span><span><span>);</span><span>#本月第几周</span></span><span>
    $tenDays</span><span>=</span><span> getTenDays</span><span>(</span><span>date</span><span>(</span><span>'j'</span><span>,</span><span>$time</span><span>));</span><span>#获得旬</span><span>
    $quarter</span><span>=</span><span> getQuarter</span><span>(</span><span>date</span><span>(</span><span>'n'</span><span>,</span><span>$time</span><span>),</span><span>date</span><span>(</span><span>'Y'</span><span>,</span><span>$time</span><span><span>));</span><span>#</span></span><span><span>获取季度</span></span><span>    $dimDate</span><span>=</span><span>array</span><span>(</span><span>'date_key'</span><span>=></span><span>strtotime</span><span>(</span><span>date</span><span>(</span><span>'Y-m-d'</span><span>,</span><span>$time</span><span>)),</span><span>#日期时间戳</span><span>'date_day'</span><span>=></span><span>date</span><span>(</span><span>'Y-m-d'</span><span>,</span><span>$time</span><span>),</span><span>#日期YYYY-MM-DD</span><span>'current_year'</span><span>=></span><span>date</span><span>(</span><span>'Y'</span><span>,</span><span>$time</span><span><span>),</span><span>#数字年</span></span><span>'current_quarter'</span><span>=></span><span>$quarter</span><span>[</span><span>'current_quarter'</span><span>],</span><span>#季度</span><span>'quarter_cn'</span><span>=></span><span>$quarter</span><span>[</span><span>'quarter_cn'</span><span>],</span><span>'current_month'</span><span>=></span><span>date</span><span>(</span><span>'n'</span><span>,</span><span>$time</span><span><span>),</span><span>#月</span></span><span>'month_cn'</span><span>=></span><span>date</span><span>(</span><span>'Y-m'</span><span>,</span><span>$time</span><span>),</span><span>#月份</span><span>'tenday_of_month'</span><span>=></span><span>$tenDays</span><span>[</span><span>'tenday_of_month'</span><span><span>],</span><span>#数字旬</span></span><span>'tenday_cn'</span><span>=></span><span>$tenDays</span><span>[</span><span>'tenday_cn'</span><span><span>],</span><span>#中文旬</span></span><span>'week_of_month'</span><span>=></span><span>ceil</span><span>(</span><span>date</span><span>(</span><span>'j'</span><span>,</span><span>$time</span><span>)/</span><span>7</span><span>),</span><span>#本月第几周</span><span>'week_of_month_cn'</span><span>=></span><span>$week_of_month_cn</span><span>[</span><span>ceil</span><span>(</span><span>date</span><span>(</span><span>'j'</span><span>,</span><span>$time</span><span>)/</span><span>7</span><span><span>)],</span><span>#中文当月第几周</span></span><span>'day_of_year'</span><span>=></span><span>date</span><span>(</span><span>'z'</span><span>,</span><span>$time</span><span>)+</span><span>1</span><span>,</span><span>#年份中的第几天</span><span>'day_of_month'</span><span>=></span><span>date</span><span>(</span><span>'j'</span><span>,</span><span>$time</span><span><span>),</span><span>#得到几号</span></span><span>'day_of_week'</span><span>=></span><span>date</span><span>(</span><span>'w'</span><span>,</span><span>$time</span><span>)></span><span>0</span><span>?</span><span>date</span><span>(</span><span>'w'</span><span>,</span><span>$time</span><span>):</span><span>7</span><span><span>,</span><span>#星期几</span></span><span>'day_of_week_cn'</span><span>=></span><span>'星期'</span><span>.</span><span>$day_of_week_cn</span><span>[</span><span>date</span><span>(</span><span>'w'</span><span>,</span><span>$time</span><span>)],</span><span>);</span><span>return</span><span>$dimDate</span><span>;</span><span>}</span><span>/**
 * 获得日期是上中下旬
 * @param  [int] $j [几号]
 * @return [array]    [description]
 * @author [yangsheng@yahoo.com]
 */</span><span>
functiongetTenDays</span><span>(</span><span>$j</span><span>)</span><span>{</span><span>    $j</span><span>=</span><span>intval</span><span>(</span><span>$j</span><span>);</span><span>if</span><span>(</span><span>$j</span><span><span>1</span><span>||</span><span>$j</span><span>></span><span>31</span><span>){</span><span>
        returnfalse</span><span><span>;</span><span>#不是日期</span></span><span>}</span><span>
   $tenDays</span><span>=</span><span>ceil</span><span>(</span><span>$j</span><span>/</span><span>10</span><span>);</span><span>switch</span><span>(</span><span>$tenDays</span><span>)</span><span>{</span><span>
        case1</span><span><span>:</span><span>#上旬</span></span><span>
            returnarray</span><span>(</span><span>'tenday_of_month'</span><span>=></span><span>1</span><span>,</span><span>'tenday_cn'</span><span>=></span><span>'上旬'</span><span>,);</span><span>break</span><span>;</span><span>
        case2</span><span><span>:</span><span>#中旬</span></span><span>
             returnarray</span><span>(</span><span>'tenday_of_month'</span><span>=></span><span>2</span><span>,</span><span>'tenday_cn'</span><span>=></span><span>'中旬'</span><span>,);</span><span>break</span><span>;</span><span>default</span><span><span>:</span><span>#下旬</span></span><span>
            returnarray</span><span>(</span><span>'tenday_of_month'</span><span>=></span><span>3</span><span>,</span><span>'tenday_cn'</span><span>=></span><span>'下旬'</span><span>,);</span><span>break</span><span>;</span><span>}</span><span>
    returnfalse</span><span>;</span><span>}</span><span>/**
 * 根据月份获得当前第几季度
 * @param  [int] $n [月份]
 * @param  [int] $y [年]
 * @return [array]    [description]
 */</span><span>
functiongetQuarter</span><span>(</span><span>$n</span><span>,</span><span>$y</span><span>=</span><span>null</span><span>){</span><span>
     $n</span><span>=</span><span>intval</span><span>(</span><span>$n</span><span>);</span><span>if</span><span>(</span><span>$n</span><span><span>1</span><span>||</span><span>$n</span><span>></span><span>12</span><span>){</span><span>
        returnfalse</span><span>;</span><span>#不是月份</span><span>}</span><span>
    $quarter</span><span>=</span><span>ceil</span><span>(</span><span>$n</span><span>/</span><span>3</span><span>);</span><span>switch</span><span>(</span><span>$quarter</span><span>)</span><span>{</span><span>
        case1</span><span>:</span><span>#第一季度</span><span>
            returnarray</span><span>(</span><span>'current_quarter'</span><span>=></span><span>1</span><span>,</span><span>'quarter_cn'</span><span>=></span><span>$y</span><span>?</span><span>$y</span><span>.</span><span>'-Q1'</span><span>:</span><span>'Q1'</span><span>);</span><span>break</span><span>;</span><span>
        case2</span><span>:</span><span>#第二季度</span><span>
            returnarray</span><span>(</span><span>'current_quarter'</span><span>=></span><span>2</span><span>,</span><span>'quarter_cn'</span><span>=></span><span>$y</span><span>?</span><span>$y</span><span>.</span><span>'-Q2'</span><span>:</span><span>'Q2'</span><span>);</span><span>break</span><span>;</span><span>
         case3</span><span>:</span><span>#第三季度</span><span>
            returnarray</span><span>(</span><span>'current_quarter'</span><span>=></span><span>3</span><span>,</span><span>'quarter_cn'</span><span>=></span><span>$y</span><span>?</span><span>$y</span><span>.</span><span>'-Q3'</span><span>:</span><span>'Q3'</span><span>);</span><span>break</span><span>;</span><span>
         case4</span><span>:</span><span>#第四季度</span><span>
            returnarray</span><span>(</span><span>'current_quarter'</span><span>=></span><span>4</span><span>,</span><span>'quarter_cn'</span><span>=></span><span>$y</span><span>?</span><span>$y</span><span>.</span><span>'-Q4'</span><span>:</span><span>'Q4'</span><span>);</span><span>break</span><span>;</span><span>}</span><span>
     returnfalse</span><span>;</span><span>}</span></span></span></span></span></span></span>
<span>网站地址:http://www.5k5c.com/blog/175</span>

以上就介绍了 php 的常用函数FUNCTION,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn