一文总结常用PHP业务逻辑demo
发布:smiling 来源: PHP粉丝网 添加日期:2023-10-06 17:22:09 浏览: 评论:0
这篇文章主要为大家介绍了常用PHP业务逻辑的demo总结,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪。
项目开发常用业务逻辑代码
项目开发中经常用到的业务逻辑代码,总结在此处,备用。
1、加密解密函数:
- function encryptDecrypt($key, $string, $decrypt){
- if($decrypt){
- $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12");
- return $decrypted;
- }else{
- $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
- return $encrypted;
- }
- }
在使用的时候我们可以为加密的字符串,设置一个加密KEY值,解密的时候传入这个KEY值即可获得密文原文。$decrypt取值0和1时分别对应加密、解密。
2、上传图片方向纠正:
由于每个人拍照姿势都不一样,难免有人拍照上传的图片出现倒立的情况,下面这个业务逻辑可以自动纠正上传图片的方向。
- $image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
- $exif = exif_read_data($_FILES['image_upload']['tmp_name']);
- if(!emptyempty($exif['Orientation'])) {
- switch($exif['Orientation']) {
- case 8:
- $image = imagerotate($image,90,0);
- break;
- case 3:
- $image = imagerotate($image,180,0);
- break;
- case 6:
- $image = imagerotate($image,-90,0);
- break;
- }
- }
3、拆分姓名中的姓氏和名字
- /**
- * 拆分姓名(姓氏和名字)
- * @param string $fullname 全名(如:百里屠苏)
- * @return array 一维数组[0=>'姓氏',1=>'名称']
- * @return array
- */
- function splitName($fullname){
- $hyphenated = array(
- '百里','北堂','北野','北宫','辟闾',
- '淳于','成公','陈生','褚师','城池',
- '端木','东方','东郭','东野','东门','第五','大狐','段干','段阳','第二','东宫',
- '公孙','公冶','公羊','公良','公西','公孟','公伯','公析','公肩','公坚','公乘','公皙','公户','公广','公仪','公祖','公玉','公仲','公上','公门','公山','高堂','高阳','郭公','谷梁','毌将','毌丘','单于','叱干','叱利','车非',
- '独孤','大野','独吉','达奚','东里',
- '哥舒','贯丘',
- '皇甫','黄龙','胡母','何阳','赫连','呼延','贺兰','贺若','黑齿','斛律','斛粟',
- '夹谷','九方','即墨','吉胡',
- '可频',
- '梁丘','闾丘','洛阳','陵尹','冷富','龙丘','令狐',
- '慕容','万俟','抹捻',
- '纳兰','南荣',
- '南宫','南郭','女娲','南伯','南容','南门','南野',
- '欧阳','欧侯',
- '濮阳','普周','仆固','仆散','蒲察',
- '青阳','漆雕','亓官','渠丘','屈突','屈卢','钳耳',
- '壤驷','汝嫣',
- '上官','少室','少叔','司徒','司马','司空','司寇','士孙','申屠','申徒','申鲜','申叔','夙沙','叔先','叔仲','叔孙','侍其','是云','索卢','厍狄',
- '澹台','太史','太叔','太公','屠岸','唐古','拓跋','同蹄','秃发',
- '闻人','巫马','微生','王孙','无庸','完颜',
- '夏侯','西门','信平','鲜于','轩辕','相里','新垣','徐离姓',
- '羊舌','羊角','延陵','於陵','伊祁','吾丘','乐正','宇文','尉迟','耶律',
- '诸葛','颛孙','仲孙','仲长','钟离','宗政','主父','中叔','左人','左丘','宰父','长儿','仉督','长孙','子车','子书','子桑'
- );
- $vLength = mb_strlen($fullname, 'utf-8');
- $lastname = '';
- $firstname = '';//前为姓,后为名
- if($vLength > 2){
- $preTwoWords = mb_substr($fullname, 0, 2, 'utf-8');//取命名的前两个字,看是否在复姓库中
- if(in_array($preTwoWords, $hyphenated)){
- $lastname = $preTwoWords;
- $firstname = mb_substr($fullname, 2, 10, 'utf-8');
- }else{
- $lastname = mb_substr($fullname, 0, 1, 'utf-8');
- $firstname = mb_substr($fullname, 1, 10, 'utf-8');
- }
- }else if($vLength == 2){//全名只有两个字时,以前一个为姓,后一下为名
- $lastname = mb_substr($fullname ,0, 1, 'utf-8');
- $firstname = mb_substr($fullname, 1, 10, 'utf-8');
- }else{
- $lastname = $fullname;
- }
- return array($lastname, $firstname);
- }
4、打印输出数据到文件
- /**
- * 打印输出数据到文件
- * @param mixed $data
- * @param bool $replace
- * @param string|null $pathname
- */
- function p($data, $replace = false, $pathname = null)
- {
- is_null($pathname) && $pathname = RUNTIME_PATH . date('Ymd') . '.txt';
- $str = (is_string($data) ? $data : (is_array($data) || is_object($data)) ? print_r($data, true) : var_export($data, true)) . "\n";
- $replace ? file_put_contents($pathname, $str) : file_put_contents($pathname, $str, FILE_APPEND);
- }
这个函数虽然很小巧,但是在调试一些内部执行数据的时候非常有用,且很方便的输出我们要查看的数据。
5、随机字符生成
- /*
- * 随机字符生成
- */
- function randStr($len=6,$format='all') {
- switch($format) {
- case 'all':
- $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~'; break;
- case 'char':
- $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~'; break;
- case 'number':
- $chars='0123456789'; break;
- case 'charnum':
- $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- break;
- case 'verify':
- $chars='ABCDEFGHIJKMNPQRSTUVWXYZ23456789';
- break;
- }
- mt_srand();
- $password="";
- while(strlen($password)<$len)
- $password.=substr($chars,(mt_rand()%strlen($chars)),1);
- return $password;
- }
6、生成唯一订单号
- /*
- * 生成唯一订单号
- */
- function CreateOrderId(){
- $order_id_main = date('YmdHis') . rand(10000000,99999999);
- //订单号码主体长度
- $order_id_len = strlen($order_id_main);
- $order_id_sum = 0;
- for($i=0; $i<$order_id_len; $i++){
- $order_id_sum += (int)(substr($order_id_main,$i,1));
- }
- //唯一订单号码(YYYYMMDDHHIISSNNNNNNNNCC)
- $order_id = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
- return $order_id;
- }
7、获取毫秒级时间戳。
关于获取毫秒级时间戳其实只需要一行代码,网上很多人专门写了很多函数去处理。因为毫秒级的时间戳是13位,int类会溢出。操作使用时可以以float类型或string类型均可。
$timestamp = substr(microtime(true)*1000,0,13);
8、获取文章中的图片
- /*
- * 获取文章图片
- */
- function getContentPic($content,$num='1'){
- $pattern="/<img.*?src=[\'|\"](.*?)[\'|\"].*?[\/]?>/";
- preg_match_all($pattern,htmlspecialchars_decode($content),$match);
- if(!emptyempty($match[1]) && $num == 1){
- return $match[1][0];
- }else{
- return $match[1];
- }
- return '';
- }
9、重写PHP array_push实现自由添加数组键值
- /*
- * 重写array_push
- * @param $array array 目标数组
- * @param $value mix 键值
- * @param $position int 索引位置
- * @param $key string 键名
- * @return Array
- */
- function array_push_position(&$array, $value=0, $position, $key = null)
- {
- $count = 0;
- $return = array();
- foreach ($array as $k => $v)
- {
- if ($count == $position)
- {
- if (!$key) $key = $count;
- $return[$key] = $value;
- $inserted = true;
- }
- $return[$k] = $v;
- $count++;
- }
- if (!$key) $key = $count;
- if (!$inserted) $return[$key];
- $array = $return;
- return $array;
- }
10、修改XML文件节点
(可以很方便的生成网站地图,方便SEO优化)
- /*
- * Xml文件增加节点
- * @param $filename string 文件路径
- * @param $data array 追加数组
- * @param $nodename string 节点名称
- * @return boole
- */
- function addXmlNode($filename,$data,$nodename){
- if(is_array($data) && !emptyempty($data) && is_file($filename) && is_string($nodename)){
- $contents = file_get_contents($filename);
- $xml = @simplexml_load_string($contents);
- foreach($data as $key=>$value){
- foreach($data[$key] as $k=>$v){
- $item = $xml->addChild($nodename);
- $item->addChild($k,$v);
- }
- }
- $xml->asXML();
- $xmlDocument = new DOMDocument('1.0');
- $xmlDocument->preserveWhiteSpace = false;
- $xmlDocument->formatOutput = true;
- $xmlDocument->loadXML($xml->asXML(),LIBXML_NOERROR);
- $res = file_put_contents($filename,$xmlDocument->saveXML());
- return $res !== false?true:$res;
- }else{
- throw new \think\Exception('Incorrect parameters');
- }
- }
提示:上述方法基于thinkPHP的异常抛出,如果非thinkPHP框架记得自行修改上述代码,以免报错。我懒得改了。
11、uuid生成
生成UUID的格式:8-4-4-4-12
- function createUUID(){
- $chars = md5(uniqid(mt_rand(), true));
- $uuid = substr ( $chars, 0, 8 ) . '-'
- . substr ( $chars, 8, 4 ) . '-'
- . substr ( $chars, 12, 4 ) . '-'
- . substr ( $chars, 16, 4 ) . '-'
- . substr ( $chars, 20, 12 );
- return $uuid ;
- }
12、计算两个日期中间相差多少年多少月多少天多少小时
- function diffTimes($stime=null){
- $stime = emptyempty($stime)?time():strtotime($stime);
- $now = time();
- //$dateMonthOne = explode('-', date('Y-m', $stime));
- //$dateMonthTwo = explode('-', date('Y-m', $now));
- $diff = [];
- $diff['hours'] = ($now - $stime) / (60 * 60);
- //$diff['day'] = ($now - $stime) / (60 * 60 * 24);
- //$diff['month'] = abs($dateMonthOne[0] - $dateMonthTwo[0]) * 12 + abs($dateMonthOne[1] - $dateMonthTwo[1]);
- //$diff['year'] = date('Y', $now) - date('Y', $stime);
- return intval($diff['hours'])."小时";
- }
13、分钟数转时分
- function min2hour($min){
- $hours = intval($min/60);
- $minutes = $min % 60;
- return $hours."小时".$minutes."分";
- }
14、时间友好展示
刚刚、几分钟前、几天前、几年前
- function mdate($time = NULL) {
- $text = '';
- $time = $time === NULL || $time > time() ? time() : intval($time);
- $t = time() - $time; //时间差 (秒)
- $y = date('Y', $time)-date('Y', time());//是否跨年
- switch($t){
- case $t == 0:
- $text = '刚刚';
- break;
- case $t < 60:
- $text = $t . '秒前'; // 一分钟内
- break;
- case $t < 60 * 60:
- $text = floor($t / 60) . '分钟前'; //一小时内
- break;
- case $t < 60 * 60 * 24:
- $text = floor($t / (60 * 60)) . '小时前'; // 一天内
- break;
- case $t < 60 * 60 * 24 * 3:
- $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天
- break;
- case $t < 60 * 60 * 24 * 30:
- $text = date('m-d H:i', $time); //一个月内
- break;
- case $t < 60 * 60 * 24 * 365&&$y==0:
- $text = date('m-d', $time); //一年内
- break;
- default:
- $text = date('Y-m-d', $time); //一年以前
- break;
- }
- return $text;
- }
15、隐藏字符串中间一部分,用*代替
- function hideStr($string, $bengin=0, $len = 4, $type = 0, $glue = "@") {
- if (emptyempty($string))
- return false;
- $array = array();
- if ($type == 0 || $type == 1 || $type == 4) {
- $strlen = $length = mb_strlen($string);
- while ($strlen) {
- $array[] = mb_substr($string, 0, 1, "utf8");
- $string = mb_substr($string, 1, $strlen, "utf8");
- $strlen = mb_strlen($string);
- }
- }
- if ($type == 0) {
- for ($i = $bengin; $i < ($bengin + $len); $i++) {
- if (isset($array[$i]))
- $array[$i] = "*";
- }
- $string = implode("", $array);
- }else if ($type == 1) {
- $array = array_reverse($array);
- for ($i = $bengin; $i < ($bengin + $len); $i++) {
- if (isset($array[$i]))
- $array[$i] = "*";
- }
- $string = implode("", array_reverse($array));
- }else if ($type == 2) {
- $array = explode($glue, $string);
- $array[0] = hideStr($array[0], $bengin, $len, 1);
- $string = implode($glue, $array);
- } else if ($type == 3) {
- $array = explode($glue, $string);
- $array[1] = hideStr($array[1], $bengin, $len, 0);
- $string = implode($glue, $array);
- } else if ($type == 4) {
- $left = $bengin;
- $right = $len;
- $tem = array();
- for ($i = 0; $i < ($length - $right); $i++) {
- if (isset($array[$i]))
- $tem[] = $i >= $left ? "*" : $array[$i];
- }
- $array = array_chunk(array_reverse($array), $right);
- $array = array_reverse($array[0]);
- for ($i = 0; $i < $right; $i++) {
- $tem[] = $array[$i];
- }
- $string = implode("", $tem);
- }
- return $string;
- }
使用说明:
第一个参数是需要转换的字符串;
第二个参数是隐藏的起始位置,当type=4时表示左侧保留长度;
第三个参数是需要隐藏的字符串个数(长度)当type=4时表示右侧保留长度;
第四个参数type取值所代表的意义:0,从左向右隐藏;1,从右向左隐藏;2,从指定字符位置分割前由右向左隐藏;3,从指定字符位置分割后由左向右隐藏;4,保留首末指定字符串。
第五个参数是分隔符。
16、二维数组按指定键排序
- function array_msort($array, $cols)
- {
- $colarr = array();
- foreach ($cols as $col => $order) {
- $colarr[$col] = array();
- foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
- }
- $eval = 'array_multisort(';
- foreach ($cols as $col => $order) {
- $eval .= '$colarr[\''.$col.'\'],'.$order.',';
- }
- $eval = substr($eval,0,-1).');';
- eval($eval);
- $ret = array();
- foreach ($colarr as $col => $arr) {
- foreach ($arr as $k => $v) {
- $k = substr($k,1);
- if (!isset($ret[$k])) $ret[$k] = $array[$k];
- $ret[$k][$col] = $array[$k][$col];
- }
- }
- return $ret;
- }
17、Base64图片流转图片文件
- function saveBase64Image($base64_image_content,$image_path){
- if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
- //图片后缀
- $type = $result[2];
- if($type=='jpeg'){
- $type='jpg';
- }
- //保存位置--图片名
- $image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
- //$image_path = '/static/upload/quanzi/'.$memberid.'/image/';
- $image_url = $image_path.$image_name;
- if(!is_dir(ROOT_PATH.$image_path)){
- mkdir(ROOT_PATH.$image_path,0755,true);
- }
- //解码
- $decode=base64_decode(str_replace($result[1], '', $base64_image_content));
- if (file_put_contents(ROOT_PATH.$image_url, $decode)){
- $data['code']='0';
- $data['imageName']=$image_name;
- $data['image_url']=$image_url;
- $data['type']=$type;
- $data['msg']='保存成功!';
- }else{
- $data['code']='1';
- $data['imgageName']='';
- $data['image_url']='';
- $data['type']='';
- $data['msg']='图片保存失败!';
- }
- }else{
- $data['code']='1';
- $data['imgageName']='';
- $data['image_url']='';
- $data['type']='';
- $data['msg']='base64图片格式有误!';
- }
- return $data;
- }
18、随机昵称
- function randnickname(){
- $tou=array('快乐','冷静','醉熏','潇洒','糊涂','积极','冷酷','深情','粗暴','温柔','可爱','愉快','义气','认真','威武','帅气','传统','潇洒','漂亮','自然','专一','听话','昏睡','狂野','等待','搞怪','幽默','魁梧','活泼','开心','高兴','超帅','留胡子','坦率','直率','轻松','痴情','完美','精明','无聊','有魅力','丰富','繁荣','饱满','炙热','暴躁','碧蓝','俊逸','英勇','健忘','故意','无心','土豪','朴实','兴奋','幸福','淡定','不安','阔达','孤独','独特','疯狂','时尚','落后','风趣','忧伤','大胆','爱笑','矮小','健康','合适','玩命','沉默','斯文','香蕉','苹果','鲤鱼','鳗鱼','任性','细心','粗心','大意','甜甜','酷酷','健壮','英俊','霸气','阳光','默默','大力','孝顺','忧虑','着急','紧张','善良','凶狠','害怕','重要','危机','欢喜','欣慰','满意','跳跃','诚心','称心','如意','怡然','娇气','无奈','无语','激动','愤怒','美好','感动','激情','激昂','震动','虚拟','超级','寒冷','精明','明理','犹豫','忧郁','寂寞','奋斗','勤奋','现代','过时','稳重','热情','含蓄','开放','无辜','多情','纯真','拉长','热心','从容','体贴','风中','曾经','追寻','儒雅','优雅','开朗','外向','内向','清爽','文艺','长情','平常','单身','伶俐','高大','懦弱','柔弱','爱笑','乐观','耍酷','酷炫','神勇','年轻','唠叨','瘦瘦','无情','包容','顺心','畅快','舒适','靓丽','负责','背后','简单','谦让','彩色','缥缈','欢呼','生动','复杂','慈祥','仁爱','魔幻','虚幻','淡然','受伤','雪白','高高','糟糕','顺利','闪闪','羞涩','缓慢','迅速','优秀','聪明','含糊','俏皮','淡淡','坚强','平淡','欣喜','能干','灵巧','友好','机智','机灵','正直','谨慎','俭朴','殷勤','虚心','辛勤','自觉','无私','无限','踏实','老实','现实','可靠','务实','拼搏','个性','粗犷','活力','成就','勤劳','单纯','落寞','朴素','悲凉','忧心','洁净','清秀','自由','小巧','单薄','贪玩','刻苦','干净','壮观','和谐','文静','调皮','害羞','安详','自信','端庄','坚定','美满','舒心','温暖','专注','勤恳','美丽','腼腆','优美','甜美','甜蜜','整齐','动人','典雅','尊敬','舒服','妩媚','秀丽','喜悦','甜美','彪壮','强健','大方','俊秀','聪慧','迷人','陶醉','悦耳','动听','明亮','结实','魁梧','标致','清脆','敏感','光亮','大气','老迟到','知性','冷傲','呆萌','野性','隐形','笑点低','微笑','笨笨','难过','沉静','火星上','失眠','安静','纯情','要减肥','迷路','烂漫','哭泣','贤惠','苗条','温婉','发嗲','会撒娇','贪玩','执着','眯眯眼','花痴','想人陪','眼睛大','高贵','傲娇','心灵美','爱撒娇','细腻','天真','怕黑','感性','飘逸','怕孤独','忐忑','高挑','傻傻','冷艳','爱听歌','还单身','怕孤单','懵懂');
- $do = array("的","爱","","与","给","扯","和","用","方","打","就","迎","向","踢","笑","闻","有","等于","保卫","演变");
- $wei=array('嚓茶','凉面','便当','毛豆','花生','可乐','灯泡','哈密瓜','野狼','背包','眼神','缘分','雪碧','人生','牛排','蚂蚁','飞鸟','灰狼','斑马','汉堡','悟空','巨人','绿茶','自行车','保温杯','大碗','墨镜','魔镜','煎饼','月饼','月亮','星星','芝麻','啤酒','玫瑰','大叔','小伙','哈密瓜,数据线','太阳','树叶','芹菜','黄蜂','蜜粉','蜜蜂','信封','西装','外套','裙子','大象','猫咪','母鸡','路灯','蓝天','白云','星月','彩虹','微笑','摩托','板栗','高山','大地','大树','电灯胆','砖头','楼房','水池','鸡翅','蜻蜓','红牛','咖啡','机器猫','枕头','大船','诺言','钢笔','刺猬','天空','飞机','大炮','冬天','洋葱','春天','夏天','秋天','冬日','航空','毛衣','豌豆','黑米','玉米','眼睛','老鼠','白羊','帅哥','美女','季节','鲜花','服饰','裙子','白开水','秀发','大山','火车','汽车','歌曲','舞蹈','老师','导师','方盒','大米','麦片','水杯','水壶','手套','鞋子','自行车','鼠标','手机','电脑','书本','奇迹','身影','香烟','夕阳','台灯','宝贝','未来','皮带','钥匙','心锁','故事','花瓣','滑板','画笔','画板','学姐','店员','电源','饼干','宝马','过客','大白','时光','石头','钻石','河马','犀牛','西牛','绿草','抽屉','柜子','往事','寒风','路人','橘子','耳机','鸵鸟','朋友','苗条','铅笔','钢笔','硬币','热狗','大侠','御姐','萝莉','毛巾','期待','盼望','白昼','黑夜','大门','黑裤','钢铁侠','哑铃','板凳','枫叶','荷花','乌龟','仙人掌','衬衫','大神','草丛','早晨','心情','茉莉','流沙','蜗牛','战斗机','冥王星','猎豹','棒球','篮球','乐曲','电话','网络','世界','中心','鱼','鸡','狗','老虎','鸭子','雨','羽毛','翅膀','外套','火','丝袜','书包','钢笔','冷风','八宝粥','烤鸡','大雁','音响','招牌','胡萝卜','冰棍','帽子','菠萝','蛋挞','香水','泥猴桃','吐司','溪流','黄豆','樱桃','小鸽子','小蝴蝶','爆米花','花卷','小鸭子','小海豚','日记本','小熊猫','小懒猪','小懒虫','荔枝','镜子','曲奇','金针菇','小松鼠','小虾米','酒窝','紫菜','金鱼','柚子','果汁','百褶裙','项链','帆布鞋','火龙果','奇异果','煎蛋','唇彩','小土豆','高跟鞋','戒指','雪糕','睫毛','铃铛','手链','香氛','红酒','月光','酸奶','银耳汤','咖啡豆','小蜜蜂','小蚂蚁','蜡烛','棉花糖','向日葵','水蜜桃','小蝴蝶','小刺猬','小丸子','指甲油','康乃馨','糖豆','薯片','口红','超短裙','乌冬面','冰淇淋','棒棒糖','长颈鹿','豆芽','发箍','发卡','发夹','发带','铃铛','小馒头','小笼包','小甜瓜','冬瓜','香菇','小兔子','含羞草','短靴','睫毛膏','小蘑菇','跳跳糖','小白菜','草莓','柠檬','月饼','百合','纸鹤','小天鹅','云朵','芒果','面包','海燕','小猫咪','龙猫','唇膏','鞋垫','羊','黑猫','白猫','万宝路','金毛','山水','音响','尊云','西安');
- $tou_num=rand(0,331);
- $do_num=rand(0,19);
- $wei_num=rand(0,327);
- $type = rand(0,1);
- if($type==0){
- $username=$tou[$tou_num].$do[$do_num].$wei[$wei_num];
- }else{
- $username=$wei[$wei_num].$tou[$tou_num];
- }
- return $username; //输出生成昵称
- }
19、去除文本中指定的HTML标签
- function strip_selected_tags($text, $tags = array()){
- $args = func_get_args();
- $text = array_shift($args);
- $tags = func_num_args() > 2 ? array_diff($args,array($text)):(array)$tags;
- foreach ($tags as $tag){
- if(preg_match_all('/<'.$tag.'[^>]*>(.*)<\/'.$tag.'>/iU', $text, $found)){
- $text = str_replace($found[0],$found[1],$text);
- }
- }
- return $text;
- }
20、人民币金额友好显示:多少万、多少亿
- function transnum($num,$type){
- //type:1单位个 2单位万
- $result = array();
- $leng = pow(10,2);//小数位修改最后一个数字
- switch($type){
- case 1:
- if($num >= 10000){
- $result['num'] = intval(($num/10000)*$leng)/$leng;
- if($result['num'] >= 10000){
- $result['num'] = intval(($num/100000000)*$leng)/$leng;
- $result['rem'] = "亿";
- }else{
- $result['rem'] = "万";
- }
- }else{
- $result['num'] = intval($num*$leng)/$leng;
- $result['rem'] = "";
- }
- break;
- case 2:
- if($num >= 10000){
- $result['num'] = intval(($num/10000)*$leng)/$leng;
- $result['rem'] = "亿";
- }else{
- $result['num'] = intval($num*$leng)/$leng;
- $result['rem'] = "万";
- }
- break;
- }
- return $result['num'].$result['rem'];
- }
21、二维数组去重
- /**
- * 去掉二维数组中的重复项
- * @param $array2D 数组
- * @param $keyArray 还原时字段对应的key
- * @return array 去掉了重复项的数组
- */
- function array_unique_fb($array2D,$keyArray){
- $temp=array();
- foreach ($array2D as $v){
- $v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
- $temp[] = $v;
- }
- $temp = array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
- foreach ($temp as $k => $v){
- //$temp[$k] = explode(",",$v); //再将拆开的数组重新组装
- $temp[$k]= array_combine($keyArray ,explode(",",trim($v)));
- }
- return $temp;
- }
Tags: PHP业务逻辑 PHP逻辑demo
- 上一篇:php实现根据中奖概率抽奖的算法
- 下一篇:最后一页
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)