本文实例总结了一些在php应用开发中常用到的函数,这些函数有字符操作,文件操作及其它的一些操作了,分享给大家供大家参考。具体如下:
1) { // 多字节字符
$return .= "%u" . strtoupper(bin2hex(mb_convert_encoding($str, 'UCS-2', $encoding)));
} else {
$return .= "%" . strtoupper(bin2hex($str));
}
}
return $return;
}
/**
* php 实现 js unescape函数
* @param [type] $str [description]
* @return [type] [description]
*/
function unescape($str) {
$str = rawurldecode($str);
preg_match_all("/(?:%u.{4})|.{4};|\d+;|.+/U",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(substr($v,0,2) == "%u"){
$ar[$k] = iconv("UCS-2","utf-8//IGNORE",pack("H4",substr($v,-4)));
} elseif(substr($v,0,3) == "") {
$ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1)));
} elseif(substr($v,0,2) == "") {
echo substr($v,2,-1)."";
$ar[$k] = iconv("UCS-2","utf-8",pack("n",substr($v,2,-1)));
}
}
return join("",$ar);
}
/**
* 数字转人名币
* @param [type] $num [description]
* @return [type] [description]
*/
function num2rmb ($num) {
$c1 = "零壹贰叁肆伍陆柒捌玖";
$c2 = "分角元拾佰仟万拾佰仟亿";
$num = round($num, 2);
$num = $num * 100;
if (strlen($num) > 10) {
return "oh,sorry,the number is too long!";
}
$i = 0;
$c = "";
while (1) {
if ($i == 0) {
$n = substr($num, strlen($num)-1, 1);
} else {
$n = $num % 10;
}
$p1 = substr($c1, 3 * $n, 3);
$p2 = substr($c2, 3 * $i, 3);
if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
$c = $p1 . $p2 . $c;
} else {
$c = $p1 . $c;
}
$i = $i + 1;
$num = $num / 10;
$num = (int)$num;
if ($num == 0) {
break;
}
}
$j = 0;
$slen = strlen($c);
while ($j < $slen) {
$m = substr($c, $j, 6);
if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
$left = substr($c, 0, $j);
$right = substr($c, $j + 3);
$c = $left . $right;
$j = $j-3;
$slen = $slen-3;
}
$j = $j + 3;
}
if (substr($c, strlen($c)-3, 3) == '零') {
$c = substr($c, 0, strlen($c)-3);
} // if there is a '0' on the end , chop it out
return $c . "整";
}
/**
* 特殊的字符
* @param [type] $str [description]
* @return [type] [description]
*/
function makeSemiangle($str) {
$arr = array(
'0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
'5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
'y' => 'y', 'z' => 'z',
'(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
'】' => ']', '〖' => '[', '〗' => ']', '{' => '{', '}' => '}', '《' => '<',
'》' => '>',
'%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
';' => ';', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
'”' => '"', '“' => '"', ''' => '`', '‘' => '`', '|' => '|', '〃' => '"',
' ' => ' ','.' => '.');
return strtr($str, $arr);
}
/**
* 下载
* @param [type] $filename [description]
* @param string $dir [description]
* @return [type] [description]
*/
function downloads($filename,$dir='./'){
$filepath = $dir.$filename;
if (!file_exists($filepath)){
header("Content-type: text/html; charset=utf-8");
echo "File not found!";
exit;
} else {
$file = fopen($filepath,"r");
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($filepath));
Header("Content-Disposition: attachment; filename=".$filename);
echo fread($file, filesize($filepath));
fclose($file);
}
}
/**
* 创建一个目录树
* @param [type] $dir [description]
* @param integer $mode [description]
* @return [type] [description]
*/
function mkdirs($dir, $mode = 0777) {
if (!is_dir($dir)) {
mkdirs(dirname($dir), $mode);
return mkdir($dir, $mode);
}
return true;
}以上就是(实用篇)非常实用的PHP常用函数汇总的内容,更多相关内容请关注PHP中文网(www.php.cn)!











