ホームページ  >  記事  >  バックエンド開発  >  php、htmlのいくつかの基本関数はphp関数、excel関数、二次関数を呼び出します。

php、htmlのいくつかの基本関数はphp関数、excel関数、二次関数を呼び出します。

WBOY
WBOYオリジナル
2016-07-29 08:55:021185ブラウズ

<code><span><span><span><span><?php 
// 函数加密解密
function encryptDecrypt<span>(<span>$key</span>, <span>$string</span>, <span>$decrypt</span>)</span>{ 
    if<span>(<span>$decrypt</span>)</span>{ 
        <span>$decrypted</span> = rtrim<span>(mcrypt_decrypt<span>(MCRYPT_RIJNDAEL_256, md5<span>(<span>$key</span>)</span>, base64_decode<span>(<span>$string</span>)</span>, MCRYPT_MODE_CBC, md5<span>(md5<span>(<span>$key</span>)</span>)</span>)</span>, <span>"12"</span>)</span>; 
        return <span>$decrypted</span>; 
    }else{ 
        <span>$encrypted</span> = base64_encode<span>(mcrypt_encrypt<span>(MCRYPT_RIJNDAEL_256, md5<span>(<span>$key</span>)</span>, <span>$string</span>, MCRYPT_MODE_CBC, md5<span>(md5<span>(<span>$key</span>)</span>)</span>)</span>)</span>; 
        return <span>$encrypted</span>; 
    } 
}
//以下是将字符串“Helloweba欢迎您”分别加密和解密 
//加密: 
echo encryptDecrypt<span>(<span>'password'</span>, <span>'Helloweba欢迎您'</span>,<span>0</span>)</span>; 
//解密: 
echo <span>"<br/>"</span>;
echo encryptDecrypt<span>(<span>'password'</span>, <span>'z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk='</span>,<span>1</span>)</span>;
echo <span>"<br/>"</span>;


// 指定长度随机数
function generateRandomString<span>(<span>$length</span> = <span>10</span>)</span> { 
    <span>$characters</span> = <span>'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'</span>; 
    <span>$randomString</span> = <span>''</span>; 
    for <span>(<span>$i</span> = <span>0</span>; <span>$i</span> < <span>$length</span>; <span>$i</span>++)</span> { 
        <span>$randomString</span> .= <span>$characters</span>[rand<span>(<span>0</span>, strlen<span>(<span>$characters</span>)</span> - <span>1</span>)</span>]; 
    } 
    return <span>$randomString</span>; 
}

echo generateRandomString<span>(<span>5</span>)</span>;
echo <span>"<br/>"</span>;

// 获取文件扩展名
function getExtension<span>(<span>$filename</span>)</span>{ 
  <span>$myext</span> = substr<span>(<span>$filename</span>, strrpos<span>(<span>$filename</span>, <span>'.'</span>)</span>)</span>; 
  return str_replace<span>(<span>'.'</span>,<span>''</span>,<span>$myext</span>)</span>; 
}
<span>$filename</span> = <span>'我的文档.doc'</span>; 
echo getExtension<span>(<span>$filename</span>)</span>;
echo <span>"<br/>"</span>;

// 获取文件大小
function formatSize<span>(<span>$size</span>)</span> { 
    <span>$sizes</span> = array<span>(<span>" Bytes"</span>, <span>" KB"</span>, <span>" MB"</span>, <span>" GB"</span>, <span>" TB"</span>, <span>" PB"</span>, <span>" EB"</span>, <span>" ZB"</span>, <span>" YB"</span>)</span>; 
    if <span>(<span>$size</span> == <span>0</span>)</span> {  
        return<span>(<span>'n/a'</span>)</span>;  
    } else { 
      return <span>(round<span>(<span>$size</span>/pow<span>(<span>1024</span>, <span>(<span>$i</span> = floor<span>(log<span>(<span>$size</span>, <span>1024</span>)</span>)</span>)</span>)</span>, <span>2</span>)</span> . <span>$sizes</span>[<span>$i</span>])</span>;  
    } 
}
echo formatSize<span>(<span>12350006</span>)</span>;
 echo <span>"<br/>"</span>;
// 替换标签
function stringParser<span>(<span>$string</span>,<span>$replacer</span>)</span>{ 
    <span>$result</span> = str_replace<span>(array_keys<span>(<span>$replacer</span>)</span>, array_values<span>(<span>$replacer</span>)</span>,<span>$string</span>)</span>; 
    return <span>$result</span>; 
}

<span>$string</span> = <span>'The {b}anchor text{/b} is the {b}actual word{/b} or words used {br}to describe the link {br}itself'</span>; 
<span>$replace_array</span> = array<span>(<span>'{b}'</span> => <span>'<b>'</span>,<span>'{/b}'</span> => <span>'</b>'</span>,<span>'{br}'</span> => <span>'<br />'</span>)</span>; 

 echo <span>"替换后:"</span>.stringParser<span>(<span>$string</span>,<span>$replace_array</span>)</span>;
 echo <span>"<br/>"</span>;

 // 目录下文件
 function listDirFiles<span>(<span>$DirPath</span>)</span>{ 
    if<span>(<span>$dir</span> = opendir<span>(<span>$DirPath</span>)</span>)</span>{ 
         while<span>(<span>(<span>$file</span> = readdir<span>(<span>$dir</span>)</span>)</span>!== false)</span>{ 
                if<span>(!is_dir<span>(<span>$DirPath</span>.<span>$file</span>)</span>)</span>                { 
                    echo <span>"filename: $file<br />"</span>; 
                } 
         } 
    } 
}
listDirFiles<span>(<span>'D:/'</span>)</span>;
echo <span>"<br/>"</span>;


// PHP获取当前页面URL
function curPageURL<span>()</span> { 
    <span>$pageURL</span> = <span>'http'</span>; 
    if <span>(!empty<span>(<span>$_SERVER</span>[<span>'HTTPS'</span>])</span>)</span> {<span>$pageURL</span> .= <span>"s"</span>;} 
    <span>$pageURL</span> .= <span>"://"</span>; 
    if <span>(<span>$_SERVER</span>[<span>"SERVER_PORT"</span>] != <span>"80"</span>)</span> { 
        <span>$pageURL</span> .= <span>$_SERVER</span>[<span>"SERVER_NAME"</span>].<span>":"</span>.<span>$_SERVER</span>[<span>"SERVER_PORT"</span>].<span>$_SERVER</span>[<span>"REQUEST_URI"</span>]; 
    } else { 
        <span>$pageURL</span> .= <span>$_SERVER</span>[<span>"SERVER_NAME"</span>].<span>$_SERVER</span>[<span>"REQUEST_URI"</span>]; 
    } 
    return <span>$pageURL</span>; 
}
echo curPageURL<span>()</span>;

// 强制下载
function download<span>(<span>$filename</span>)</span>{ 
    if <span>(<span>(isset<span>(<span>$filename</span>)</span>)</span>&&<span>(file_exists<span>(<span>$filename</span>)</span>)</span>)</span>{ 
       header<span>(<span>"Content-length: "</span>.filesize<span>(<span>$filename</span>)</span>)</span>; 
       header<span>(<span>'Content-Type: application/octet-stream'</span>)</span>; 
       header<span>(<span>'Content-Disposition: attachment; filename="'</span> . <span>$filename</span> . <span>'"'</span>)</span>; 
       readfile<span>(<span>"$filename"</span>)</span>; 
    } else { 
       echo <span>"Looks like file does not exist!"</span>; 
    } 
}
// download<span>(<span>"D:/key.txt"</span>)</span>;


//获取用户真实IP 
function getIp<span>()</span> { 
    if <span>(getenv<span>(<span>"HTTP_CLIENT_IP"</span>)</span> && strcasecmp<span>(getenv<span>(<span>"HTTP_CLIENT_IP"</span>)</span>, <span>"unknown"</span>)</span>)</span><span>$ip</span> = getenv<span>(<span>"HTTP_CLIENT_IP"</span>)</span>; 
    else 
        if <span>(getenv<span>(<span>"HTTP_X_FORWARDED_FOR"</span>)</span> && strcasecmp<span>(getenv<span>(<span>"HTTP_X_FORWARDED_FOR"</span>)</span>, <span>"unknown"</span>)</span>)</span><span>$ip</span> = getenv<span>(<span>"HTTP_X_FORWARDED_FOR"</span>)</span>; 
        else 
            if <span>(getenv<span>(<span>"REMOTE_ADDR"</span>)</span> && strcasecmp<span>(getenv<span>(<span>"REMOTE_ADDR"</span>)</span>, <span>"unknown"</span>)</span>)</span><span>$ip</span> = getenv<span>(<span>"REMOTE_ADDR"</span>)</span>; 
            else 
                if <span>(isset <span>(<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>])</span> && <span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>] && strcasecmp<span>(<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>], <span>"unknown"</span>)</span>)</span><span>$ip</span> = <span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>]; 
                else 
                    <span>$ip</span> = <span>"unknown"</span>; 
    return <span>(<span>$ip</span>)</span>; 
}
echo <span>"<br/>"</span>;
echo getIp<span>()</span>;
echo <span>"<br/>"</span>;





function message<span>(<span>$msgTitle</span>,<span>$message</span>,<span>$jumpUrl</span>)</span>{ 
    <span>$str</span> = <span>'<!DOCTYPE HTML>'</span>; 
    <span>$str</span> .= <span>'<html>'</span>; 
    <span>$str</span> .= <span>'<head>'</span>; 
    <span>$str</span> .= <span>'<meta charset="utf-8">'</span>; 
    <span>$str</span> .= <span>'<title>页面提示</title>'</span>; 
    <span>$str</span> .= <span>'<style type="text/css">'</span>; 
    <span>$str</span> .= <span>'*{margin:0; padding:0}a{color:#369; text-decoration:none;}a:hover{text-decoration:underline}body{height:100%; font:12px/18px Tahoma, Arial,  sans-serif; color:#424242; background:#fff}.message{width:450px; height:120px; margin:16% auto; border:1px solid #99b1c4; background:#ecf7fb}.message h3{height:28px; line-height:28px; background:#2c91c6; text-align:center; color:#fff; font-size:14px}.msg_txt{padding:10px; margin-top:8px}.msg_txt h4{line-height:26px; font-size:14px}.msg_txt h4.red{color:#f30}.msg_txt p{line-height:22px}'</span>; 
    <span>$str</span> .= <span>'</style>'</span>; 
    <span>$str</span> .= <span>'</head>'</span>; 
    <span>$str</span> .= <span>'<body>'</span>; 
    <span>$str</span> .= <span>'<div>'</span>; 
    <span>$str</span> .= <span>'<h3>'</span>.<span>$msgTitle</span>.<span>'</h3>'</span>; 
    <span>$str</span> .= <span>'<div>'</span>; 
    <span>$str</span> .= <span>'<h4>'</span>.<span>$message</span>.<span>'</h4>'</span>; 
    <span>$str</span> .= <span>'<p>系统将在 <span>3</span> 秒后自动跳转,如果不想等待,直接点击 <a href="{$jumpUrl}">这里</a> 跳转</p>'</span>; 
    <span>$str</span> .= <span>"<script>setTimeout('location.replace('"</span>.<span>$jumpUrl</span>.<span>"')',2000)</script>"</span>; 
    <span>$str</span> .= <span>'</div>'</span>; 
    <span>$str</span> .= <span>'</div>'</span>; 
    <span>$str</span> .= <span>'</body>'</span>; 
    <span>$str</span> .= <span>'</html>'</span>; 
    echo <span>$str</span>; 
}
// message<span>(<span>'操作提示'</span>,<span>'操作成功!'</span>,<span>'http://www.baidu.com'</span>)</span>;

function getClientIp<span>()</span> {
 <span>$ip</span> = NULL;
 if <span>(isset<span>(<span>$_SERVER</span>[<span>'HTTP_X_FORWARDED_FOR'</span>])</span>)</span> {
  <span>$arr</span> = explode<span>(<span>','</span>, <span>$_SERVER</span>[<span>'HTTP_X_FORWARDED_FOR'</span>])</span>;
  <span>$pos</span> = array_search<span>(<span>'unknown'</span>,<span>$arr</span>)</span>;
  if<span>(false !== <span>$pos</span>)</span> unset<span>(<span>$arr</span>[<span>$pos</span>])</span>;
  <span>$ip</span> = trim<span>(<span>$arr</span>[<span>0</span>])</span>;
 }elseif <span>(isset<span>(<span>$_SERVER</span>[<span>'HTTP_CLIENT_IP'</span>])</span>)</span> {
  <span>$ip</span> = <span>$_SERVER</span>[<span>'HTTP_CLIENT_IP'</span>];
 }elseif <span>(isset<span>(<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>])</span>)</span> {
  <span>$ip</span> = <span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>];
 }
 // IP地址合法验证
 <span>$ip</span> = <span>(false !== ip2long<span>(<span>$ip</span>)</span>)</span> &#<span>63</span>; <span>$ip</span> : <span>'0.0.0.0'</span>;
 return <span>$ip</span>;
}

<span>/**
 * 获取在线IP
 * @return String
 */</span>
function getOnlineIp<span>(<span>$format</span>=<span>0</span>)</span> {
 global <span>$S_GLOBAL</span>;
 if<span>(empty<span>(<span>$S_GLOBAL</span>[<span>'onlineip'</span>])</span>)</span> {
  if<span>(getenv<span>(<span>'HTTP_CLIENT_IP'</span>)</span> && strcasecmp<span>(getenv<span>(<span>'HTTP_CLIENT_IP'</span>)</span>, <span>'unknown'</span>)</span>)</span> {
   <span>$onlineip</span> = getenv<span>(<span>'HTTP_CLIENT_IP'</span>)</span>;
  } elseif<span>(getenv<span>(<span>'HTTP_X_FORWARDED_FOR'</span>)</span> && strcasecmp<span>(getenv<span>(<span>'HTTP_X_FORWARDED_FOR'</span>)</span>, <span>'unknown'</span>)</span>)</span> {
   <span>$onlineip</span> = getenv<span>(<span>'HTTP_X_FORWARDED_FOR'</span>)</span>;
  } elseif<span>(getenv<span>(<span>'REMOTE_ADDR'</span>)</span> && strcasecmp<span>(getenv<span>(<span>'REMOTE_ADDR'</span>)</span>, <span>'unknown'</span>)</span>)</span> {
   <span>$onlineip</span> = getenv<span>(<span>'REMOTE_ADDR'</span>)</span>;
  } elseif<span>(isset<span>(<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>])</span> && <span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>] && strcasecmp<span>(<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>], <span>'unknown'</span>)</span>)</span> {
   <span>$onlineip</span> = <span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>];
  }
  preg_match<span>(<span>"/[\d\.]{7,15}/"</span>, <span>$onlineip</span>, <span>$onlineipmatches</span>)</span>;
  <span>$S_GLOBAL</span>[<span>'onlineip'</span>] = <span>$onlineipmatches</span>[<span>0</span>] &#<span>63</span>; <span>$onlineipmatches</span>[<span>0</span>] : <span>'unknown'</span>;
 }

 if<span>(<span>$format</span>)</span> {
  <span>$ips</span> = explode<span>(<span>'.'</span>, <span>$S_GLOBAL</span>[<span>'onlineip'</span>])</span>;
  for<span>(<span>$i</span>=<span>0</span>;<span>$i</span>&lt;<span>3</span>;<span>$i</span>++)</span> {
   <span>$ips</span>[<span>$i</span>] = intval<span>(<span>$ips</span>[<span>$i</span>])</span>;
  }
  return sprintf<span>(<span>'%03d%03d%03d'</span>, <span>$ips</span>[<span>0</span>], <span>$ips</span>[<span>1</span>], <span>$ips</span>[<span>2</span>])</span>;
 } else {
  return <span>$S_GLOBAL</span>[<span>'onlineip'</span>];
 }
}



<span>/**
 * 获取url
 * @return [type] [description]
 */</span>
function getUrl<span>()</span>{
 <span>$pageURL</span> = <span>'http'</span>;
 if <span>(isset<span>(<span>$_SERVER</span>[<span>"HTTPS"</span>])</span> && <span>$_SERVER</span>[<span>"HTTPS"</span>] == <span>"on"</span>)</span> {
 <span>$pageURL</span> .= <span>"s"</span>;
 }
 <span>$pageURL</span> .= <span>"://"</span>;
 if <span>(<span>$_SERVER</span>[<span>"SERVER_PORT"</span>] != <span>"80"</span>)</span> {
 <span>$pageURL</span> .= <span>$_SERVER</span>[<span>"HTTP_HOST"</span>] . <span>":"</span> . <span>$_SERVER</span>[<span>"SERVER_PORT"</span>] . <span>$_SERVER</span>[<span>"REQUEST_URI"</span>];
 } else {
 <span>$pageURL</span> .= <span>$_SERVER</span>[<span>"HTTP_HOST"</span>] . <span>$_SERVER</span>[<span>"REQUEST_URI"</span>];
 }
 return <span>$pageURL</span>;
}

<span>/**
 * 获取当前站点的访问路径根目录
 * @return [type] [description]
 */</span>
function getSiteUrl<span>()</span> {
 <span>$uri</span> = <span>$_SERVER</span>[<span>'REQUEST_URI'</span>]&#<span>63</span>;<span>$_SERVER</span>[<span>'REQUEST_URI'</span>]:<span>(<span>$_SERVER</span>[<span>'PHP_SELF'</span>]&#<span>63</span>;<span>$_SERVER</span>[<span>'PHP_SELF'</span>]:<span>$_SERVER</span>[<span>'SCRIPT_NAME'</span>])</span>;
 return <span>'http://'</span>.<span>$_SERVER</span>[<span>'HTTP_HOST'</span>].substr<span>(<span>$uri</span>, <span>0</span>, strrpos<span>(<span>$uri</span>, <span>'/'</span>)</span>+<span>1</span>)</span>;
}



<span>/**
 * 字符串截取,支持中文和其他编码
 * @param [string] $str  [字符串]
 * @param integer $start [起始位置]
 * @param integer $length [截取长度]
 * @param string $charset [字符串编码]
 * @param boolean $suffix [是否有省略号]
 * @return [type]   [description]
 */</span>
function msubstr<span>(<span>$str</span>, <span>$start</span>=<span>0</span>, <span>$length</span>=<span>15</span>, <span>$charset</span>=<span>"utf-8"</span>, <span>$suffix</span>=true)</span> {
 if<span>(function_exists<span>(<span>"mb_substr"</span>)</span>)</span> {
  return mb_substr<span>(<span>$str</span>, <span>$start</span>, <span>$length</span>, <span>$charset</span>)</span>;
 } elseif<span>(function_exists<span>(<span>'iconv_substr'</span>)</span>)</span> {
  return iconv_substr<span>(<span>$str</span>,<span>$start</span>,<span>$length</span>,<span>$charset</span>)</span>;
 }
 <span>$re</span>[<span>'utf-8'</span>] = <span>"/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"</span>;
 <span>$re</span>[<span>'gb2312'</span>] = <span>"/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"</span>;
 <span>$re</span>[<span>'gbk'</span>] = <span>"/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"</span>;
 <span>$re</span>[<span>'big5'</span>] = <span>"/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"</span>;
 preg_match_all<span>(<span>$re</span>[<span>$charset</span>], <span>$str</span>, <span>$match</span>)</span>;
 <span>$slice</span> = join<span>(<span>""</span>,array_slice<span>(<span>$match</span>[<span>0</span>], <span>$start</span>, <span>$length</span>)</span>)</span>;
 if<span>(<span>$suffix</span>)</span> {
  return <span>$slice</span>.<span>"…"</span>;
 }
 return <span>$slice</span>;
}

<span>/**
 * php 实现js escape 函数
 * @param [type] $string [description]
 * @param string $encoding [description]
 * @return [type]   [description]
 */</span>
function escape<span>(<span>$string</span>, <span>$encoding</span> = <span>'UTF-8'</span>)</span>{
 <span>$return</span> = null;
 for <span>(<span>$x</span> = <span>0</span>; <span>$x</span> &lt; mb_strlen<span>(<span>$string</span>, <span>$encoding</span>)</span>;<span>$x</span> ++)</span>
 {
 <span>$str</span> = mb_substr<span>(<span>$string</span>, <span>$x</span>, <span>1</span>, <span>$encoding</span>)</span>;
 if <span>(strlen<span>(<span>$str</span>)</span> &gt; <span>1</span>)</span> { // 多字节字符
  <span>$return</span> .= <span>"%u"</span> . strtoupper<span>(bin2hex<span>(mb_convert_encoding<span>(<span>$str</span>, <span>'UCS-2'</span>, <span>$encoding</span>)</span>)</span>)</span>;
 } else {
  <span>$return</span> .= <span>"%"</span> . strtoupper<span>(bin2hex<span>(<span>$str</span>)</span>)</span>;
 }
 }
 return <span>$return</span>;
}
<span>/**
 * php 实现 js unescape函数
 * @param [type] $str [description]
 * @return [type]  [description]
 */</span>
function unescape<span>(<span>$str</span>)</span> {
 <span>$str</span> = rawurldecode<span>(<span>$str</span>)</span>;
 preg_match_all<span>(<span>"/(&#63;:%u.{4})|.{4};|&#\d+;|.+/U"</span>,<span>$str</span>,<span>$r</span>)</span>;
 <span>$ar</span> = <span>$r</span>[<span>0</span>];
 foreach<span>(<span>$ar</span> as <span>$k</span>=&gt;<span>$v</span>)</span> {
  if<span>(substr<span>(<span>$v</span>,<span>0</span>,<span>2</span>)</span> == <span>"%u"</span>)</span>{
   <span>$ar</span>[<span>$k</span>] = iconv<span>(<span>"UCS-2"</span>,<span>"utf-8//IGNORE"</span>,pack<span>(<span>"H4"</span>,substr<span>(<span>$v</span>,-<span>4</span>)</span>)</span>)</span>;
  } elseif<span>(substr<span>(<span>$v</span>,<span>0</span>,<span>3</span>)</span> == <span>""</span>)</span> {
   <span>$ar</span>[<span>$k</span>] = iconv<span>(<span>"UCS-2"</span>,<span>"utf-8"</span>,pack<span>(<span>"H4"</span>,substr<span>(<span>$v</span>,<span>3</span>,-<span>1</span>)</span>)</span>)</span>;
  } elseif<span>(substr<span>(<span>$v</span>,<span>0</span>,<span>2</span>)</span> == <span>"&#"</span>)</span> {
   echo substr<span>(<span>$v</span>,<span>2</span>,-<span>1</span>)</span>.<span>""</span>;
   <span>$ar</span>[<span>$k</span>] = iconv<span>(<span>"UCS-2"</span>,<span>"utf-8"</span>,pack<span>(<span>"n"</span>,substr<span>(<span>$v</span>,<span>2</span>,-<span>1</span>)</span>)</span>)</span>;
  }
 }
 return join<span>(<span>""</span>,<span>$ar</span>)</span>;
}

<span>/**
 * 数字转人名币
 * @param [type] $num [description]
 * @return [type]  [description]
 */</span>
function num2rmb <span>(<span>$num</span>)</span> {
 <span>$c1</span> = <span>"零壹贰叁肆伍陆柒捌玖"</span>;
 <span>$c2</span> = <span>"分角元拾佰仟万拾佰仟亿"</span>;
 <span>$num</span> = round<span>(<span>$num</span>, <span>2</span>)</span>;
 <span>$num</span> = <span>$num</span> * <span>100</span>;
 if <span>(strlen<span>(<span>$num</span>)</span> &gt; <span>10</span>)</span> {
  return <span>"oh,sorry,the number is too long!"</span>;
 }
 <span>$i</span> = <span>0</span>;
 <span>$c</span> = <span>""</span>;
 while <span>(<span>1</span>)</span> {
  if <span>(<span>$i</span> == <span>0</span>)</span> {
   <span>$n</span> = substr<span>(<span>$num</span>, strlen<span>(<span>$num</span>)</span>-<span>1</span>, <span>1</span>)</span>;
  } else {
   <span>$n</span> = <span>$num</span> % <span>10</span>;
  }
  <span>$p1</span> = substr<span>(<span>$c1</span>, <span>3</span> * <span>$n</span>, <span>3</span>)</span>;
  <span>$p2</span> = substr<span>(<span>$c2</span>, <span>3</span> * <span>$i</span>, <span>3</span>)</span>;
  if <span>(<span>$n</span> != <span>'0'</span> || <span>(<span>$n</span> == <span>'0'</span> && <span>(<span>$p2</span> == <span>'亿'</span> || <span>$p2</span> == <span>'万'</span> || <span>$p2</span> == <span>'元'</span>)</span>)</span>)</span> {
   <span>$c</span> = <span>$p1</span> . <span>$p2</span> . <span>$c</span>;
  } else {
   <span>$c</span> = <span>$p1</span> . <span>$c</span>;
  }
  <span>$i</span> = <span>$i</span> + <span>1</span>;
  <span>$num</span> = <span>$num</span> / <span>10</span>;
  <span>$num</span> = <span>(int)</span><span>$num</span>;
  if <span>(<span>$num</span> == <span>0</span>)</span> {
   break;
  }
 }
 <span>$j</span> = <span>0</span>;
 <span>$slen</span> = strlen<span>(<span>$c</span>)</span>;
 while <span>(<span>$j</span> &lt; <span>$slen</span>)</span> {
  <span>$m</span> = substr<span>(<span>$c</span>, <span>$j</span>, <span>6</span>)</span>;
  if <span>(<span>$m</span> == <span>'零元'</span> || <span>$m</span> == <span>'零万'</span> || <span>$m</span> == <span>'零亿'</span> || <span>$m</span> == <span>'零零'</span>)</span> {
   <span>$left</span> = substr<span>(<span>$c</span>, <span>0</span>, <span>$j</span>)</span>;
   <span>$right</span> = substr<span>(<span>$c</span>, <span>$j</span> + <span>3</span>)</span>;
   <span>$c</span> = <span>$left</span> . <span>$right</span>;
   <span>$j</span> = <span>$j</span>-<span>3</span>;
   <span>$slen</span> = <span>$slen</span>-<span>3</span>;
  }
  <span>$j</span> = <span>$j</span> + <span>3</span>;
 }
 if <span>(substr<span>(<span>$c</span>, strlen<span>(<span>$c</span>)</span>-<span>3</span>, <span>3</span>)</span> == <span>'零'</span>)</span> {
  <span>$c</span> = substr<span>(<span>$c</span>, <span>0</span>, strlen<span>(<span>$c</span>)</span>-<span>3</span>)</span>;
 } // if there is a <span>'0'</span> on the end , chop it out
 return <span>$c</span> . <span>"整"</span>;
}

<span>/**
 * 特殊的字符
 * @param [type] $str [description]
 * @return [type]  [description]
 */</span>
function makeSemiangle<span>(<span>$str</span>)</span> {
 <span>$arr</span> = array<span>(
  <span>'0'</span> =&gt; <span>'0'</span>, <span>'1'</span> =&gt; <span>'1'</span>, <span>'2'</span> =&gt; <span>'2'</span>, <span>'3'</span> =&gt; <span>'3'</span>, <span>'4'</span> =&gt; <span>'4'</span>,
  <span>'5'</span> =&gt; <span>'5'</span>, <span>'6'</span> =&gt; <span>'6'</span>, <span>'7'</span> =&gt; <span>'7'</span>, <span>'8'</span> =&gt; <span>'8'</span>, <span>'9'</span> =&gt; <span>'9'</span>,
  <span>'A'</span> =&gt; <span>'A'</span>, <span>'B'</span> =&gt; <span>'B'</span>, <span>'C'</span> =&gt; <span>'C'</span>, <span>'D'</span> =&gt; <span>'D'</span>, <span>'E'</span> =&gt; <span>'E'</span>,
  <span>'F'</span> =&gt; <span>'F'</span>, <span>'G'</span> =&gt; <span>'G'</span>, <span>'H'</span> =&gt; <span>'H'</span>, <span>'I'</span> =&gt; <span>'I'</span>, <span>'J'</span> =&gt; <span>'J'</span>,
  <span>'K'</span> =&gt; <span>'K'</span>, <span>'L'</span> =&gt; <span>'L'</span>, <span>'M'</span> =&gt; <span>'M'</span>, <span>'N'</span> =&gt; <span>'N'</span>, <span>'O'</span> =&gt; <span>'O'</span>,
  <span>'P'</span> =&gt; <span>'P'</span>, <span>'Q'</span> =&gt; <span>'Q'</span>, <span>'R'</span> =&gt; <span>'R'</span>, <span>'S'</span> =&gt; <span>'S'</span>, <span>'T'</span> =&gt; <span>'T'</span>,
  <span>'U'</span> =&gt; <span>'U'</span>, <span>'V'</span> =&gt; <span>'V'</span>, <span>'W'</span> =&gt; <span>'W'</span>, <span>'X'</span> =&gt; <span>'X'</span>, <span>'Y'</span> =&gt; <span>'Y'</span>,
  <span>'Z'</span> =&gt; <span>'Z'</span>, <span>'a'</span> =&gt; <span>'a'</span>, <span>'b'</span> =&gt; <span>'b'</span>, <span>'c'</span> =&gt; <span>'c'</span>, <span>'d'</span> =&gt; <span>'d'</span>,
  <span>'e'</span> =&gt; <span>'e'</span>, <span>'f'</span> =&gt; <span>'f'</span>, <span>'g'</span> =&gt; <span>'g'</span>, <span>'h'</span> =&gt; <span>'h'</span>, <span>'i'</span> =&gt; <span>'i'</span>,
  <span>'j'</span> =&gt; <span>'j'</span>, <span>'k'</span> =&gt; <span>'k'</span>, <span>'l'</span> =&gt; <span>'l'</span>, <span>'m'</span> =&gt; <span>'m'</span>, <span>'n'</span> =&gt; <span>'n'</span>,
  <span>'o'</span> =&gt; <span>'o'</span>, <span>'p'</span> =&gt; <span>'p'</span>, <span>'q'</span> =&gt; <span>'q'</span>, <span>'r'</span> =&gt; <span>'r'</span>, <span>'s'</span> =&gt; <span>'s'</span>,
  <span>'t'</span> =&gt; <span>'t'</span>, <span>'u'</span> =&gt; <span>'u'</span>, <span>'v'</span> =&gt; <span>'v'</span>, <span>'w'</span> =&gt; <span>'w'</span>, <span>'x'</span> =&gt; <span>'x'</span>,
  <span>'y'</span> =&gt; <span>'y'</span>, <span>'z'</span> =&gt; <span>'z'</span>,
  <span>'('</span> =&gt; <span>'('</span>, <span>')'</span> =&gt; <span>')'</span>, <span>'〔'</span> =&gt; <span>'['</span>, <span>'〕'</span> =&gt; <span>']'</span>, <span>'【'</span> =&gt; <span>'['</span>,
  <span>'】'</span> =&gt; <span>']'</span>, <span>'〖'</span> =&gt; <span>'['</span>, <span>'〗'</span> =&gt; <span>']'</span>, <span>'{'</span> =&gt; <span>'{'</span>, <span>'}'</span> =&gt; <span>'}'</span>, <span>'《'</span> =&gt; <span>'&lt;'</span>,
  <span>'》'</span> =&gt; <span>'&gt;'</span>,
  <span>'%'</span> =&gt; <span>'%'</span>, <span>'+'</span> =&gt; <span>'+'</span>, <span>'—'</span> =&gt; <span>'-'</span>, <span>'-'</span> =&gt; <span>'-'</span>, <span>'~'</span> =&gt; <span>'-'</span>,
  <span>':'</span> =&gt; <span>':'</span>, <span>'。'</span> =&gt; <span>'.'</span>, <span>'、'</span> =&gt; <span>','</span>, <span>','</span> =&gt; <span>'.'</span>, <span>'、'</span> =&gt; <span>'.'</span>,
  <span>';'</span> =&gt; <span>';'</span>, <span>'?'</span> =&gt; <span>'&#63;'</span>, <span>'!'</span> =&gt; <span>'!'</span>, <span>'…'</span> =&gt; <span>'-'</span>, <span>'‖'</span> =&gt; <span>'|'</span>,
  <span>'”'</span> =&gt; <span>'"'</span>, <span>'“'</span> =&gt; <span>'"'</span>, <span>''</span> =&gt; <span>'`'</span>, <span>'‘'</span> =&gt; <span>'`'</span>, <span>'|'</span> =&gt; <span>'|'</span>, <span>'〃'</span> =&gt; <span>'"'</span>,
  <span>' '</span> =&gt; <span>' '</span>,<span>'.'</span> =&gt; <span>'.'</span>)</span>;
 return strtr<span>(<span>$str</span>, <span>$arr</span>)</span>;
}

<span>/**
 * 下载
 * @param [type] $filename [description]
 * @param string $dir  [description]
 * @return [type]   [description]
 */</span>
function downloads<span>(<span>$filename</span>,<span>$dir</span>=<span>'./'</span>)</span>{
 <span>$filepath</span> = <span>$dir</span>.<span>$filename</span>;
 if <span>(!file_exists<span>(<span>$filepath</span>)</span>)</span>{
  header<span>(<span>"Content-type: text/html; charset=utf-8"</span>)</span>;
  echo <span>"File not found!"</span>;
  exit;
 } else {
  <span>$file</span> = fopen<span>(<span>$filepath</span>,<span>"r"</span>)</span>;
  Header<span>(<span>"Content-type: application/octet-stream"</span>)</span>;
  Header<span>(<span>"Accept-Ranges: bytes"</span>)</span>;
  Header<span>(<span>"Accept-Length: "</span>.filesize<span>(<span>$filepath</span>)</span>)</span>;
  Header<span>(<span>"Content-Disposition: attachment; filename="</span>.<span>$filename</span>)</span>;
  echo fread<span>(<span>$file</span>, filesize<span>(<span>$filepath</span>)</span>)</span>;
  fclose<span>(<span>$file</span>)</span>;
 }
}

<span>/**
 * 创建一个目录树
 * @param [type] $dir [description]
 * @param integer $mode [description]
 * @return [type]  [description]
 */</span>
function mkdirs<span>(<span>$dir</span>, <span>$mode</span> = <span>0777</span>)</span> {
 if <span>(!is_dir<span>(<span>$dir</span>)</span>)</span> {
  mkdirs<span>(dirname<span>(<span>$dir</span>)</span>, <span>$mode</span>)</span>;
  return mkdir<span>(<span>$dir</span>, <span>$mode</span>)</span>;
 }
 return true;
}

 ?></span></span></span></code>

本文转载至http://www.jb51.net/article/58610.htm

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。