首页
关于
归档
朋友
壁纸
留言
API平台
告白墙
更多
休闲游戏
留言板
练字贴
Layui手册
Search
1
【PHP】PHPoffice/PHPSpreadsheet读取和写入Excel
1,339 阅读
2
【Git】No tracked branch configured for branch master or the branch doesn't exist.
1,039 阅读
3
【Layui】控制页面元素展示隐藏
860 阅读
4
【composer】composer常用命令
787 阅读
5
【PHP】PHP实现JWT生成和验证
769 阅读
默认分类
PHP
ThinkPHP
Laravel
面向对象
设计模式
算法
基础
网络安全
Web
HTML
CSS
JavaScript
jQuery
Layui
VUE
uni-app
Database
MySQL
Redis
RabbitMQ
Nginx
Git
Linux
Soft Ware
Windows
网赚
Go
登录
Search
标签搜索
PHP
函数
方法
类
MySQL
ThinkPHP
OOP
JavaScript
Layui
Web
Linux
Array
设计模式
Git
PHPSpreadsheet
PHPoffice
排序算法
基础
面试题
Windows
小破孩
累计撰写
223
篇文章
累计收到
33
条评论
首页
栏目
默认分类
PHP
ThinkPHP
Laravel
面向对象
设计模式
算法
基础
网络安全
Web
HTML
CSS
JavaScript
jQuery
Layui
VUE
uni-app
Database
MySQL
Redis
RabbitMQ
Nginx
Git
Linux
Soft Ware
Windows
网赚
Go
页面
关于
归档
朋友
壁纸
留言
API平台
告白墙
休闲游戏
留言板
练字贴
Layui手册
搜索到
64
篇与
的结果
2022-06-21
【PHP】生成avatar头像
/** * @Author: 小破孩嫩 * @Email: 3584685883@qq.com * @Time: 2020/12/10 20:00 * @param string $email 邮箱 * @param int $s 大小 * @param string $d * @param string $g * @return string * @Description:生成avatar头像 */ function getAvatar($email='', $s=40, $d='mm', $g='g') { $hash = md5($email); $avatar = "http://www.gravatar.com/avatar/$hash?s=$s&d=$d&r=$g"; return $avatar; }
2022年06月21日
158 阅读
0 评论
0 点赞
2022-06-21
【PHP】获取文件MIME类型-根据文件扩展名来获取
function getFileType($ext){ static $mime_types = [ 'apk' => 'application/vnd.android.package-archive', '3gp' => 'video/3gpp', 'ai' => 'application/postscript', 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff', 'aiff' => 'audio/x-aiff', 'asc' => 'text/plain', 'atom' => 'application/atom+xml', 'au' => 'audio/basic', 'avi' => 'video/x-msvideo', 'bcpio' => 'application/x-bcpio', 'bin' => 'application/octet-stream', 'bmp' => 'image/bmp', 'cdf' => 'application/x-netcdf', 'cgm' => 'image/cgm', 'class' => 'application/octet-stream', 'cpio' => 'application/x-cpio', 'cpt' => 'application/mac-compactpro', 'csh' => 'application/x-csh', 'css' => 'text/css', 'dcr' => 'application/x-director', 'dif' => 'video/x-dv', 'dir' => 'application/x-director', 'djv' => 'image/vnd.djvu', 'djvu' => 'image/vnd.djvu', 'dll' => 'application/octet-stream', 'dmg' => 'application/octet-stream', 'dms' => 'application/octet-stream', 'doc' => 'application/msword', 'dtd' => 'application/xml-dtd', 'dv' => 'video/x-dv', 'dvi' => 'application/x-dvi', 'dxr' => 'application/x-director', 'eps' => 'application/postscript', 'etx' => 'text/x-setext', 'exe' => 'application/octet-stream', 'ez' => 'application/andrew-inset', 'flv' => 'video/x-flv', 'gif' => 'image/gif', 'gram' => 'application/srgs', 'grxml' => 'application/srgs+xml', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip', 'hdf' => 'application/x-hdf', 'hqx' => 'application/mac-binhex40', 'htm' => 'text/html', 'html' => 'text/html', 'ice' => 'x-conference/x-cooltalk', 'ico' => 'image/x-icon', 'ics' => 'text/calendar', 'ief' => 'image/ief', 'ifb' => 'text/calendar', 'iges' => 'model/iges', 'igs' => 'model/iges', 'jnlp' => 'application/x-java-jnlp-file', 'jp2' => 'image/jp2', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'js' => 'application/x-javascript', 'kar' => 'audio/midi', 'latex' => 'application/x-latex', 'lha' => 'application/octet-stream', 'lzh' => 'application/octet-stream', 'm3u' => 'audio/x-mpegurl', 'm4a' => 'audio/mp4a-latm', 'm4p' => 'audio/mp4a-latm', 'm4u' => 'video/vnd.mpegurl', 'm4v' => 'video/x-m4v', 'mac' => 'image/x-macpaint', 'man' => 'application/x-troff-man', 'mathml' => 'application/mathml+xml', 'me' => 'application/x-troff-me', 'mesh' => 'model/mesh', 'mid' => 'audio/midi', 'midi' => 'audio/midi', 'mif' => 'application/vnd.mif', 'mov' => 'video/quicktime', 'movie' => 'video/x-sgi-movie', 'mp2' => 'audio/mpeg', 'mp3' => 'audio/mpeg', 'mp4' => 'video/mp4', 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mpga' => 'audio/mpeg', 'ms' => 'application/x-troff-ms', 'msh' => 'model/mesh', 'mxu' => 'video/vnd.mpegurl', 'nc' => 'application/x-netcdf', 'oda' => 'application/oda', 'ogg' => 'application/ogg', 'ogv' => 'video/ogv', 'pbm' => 'image/x-portable-bitmap', 'pct' => 'image/pict', 'pdb' => 'chemical/x-pdb', 'pdf' => 'application/pdf', 'pgm' => 'image/x-portable-graymap', 'pgn' => 'application/x-chess-pgn', 'pic' => 'image/pict', 'pict' => 'image/pict', 'png' => 'image/png', 'pnm' => 'image/x-portable-anymap', 'pnt' => 'image/x-macpaint', 'pntg' => 'image/x-macpaint', 'ppm' => 'image/x-portable-pixmap', 'ppt' => 'application/vnd.ms-powerpoint', 'ps' => 'application/postscript', 'qt' => 'video/quicktime', 'qti' => 'image/x-quicktime', 'qtif' => 'image/x-quicktime', 'ra' => 'audio/x-pn-realaudio', 'ram' => 'audio/x-pn-realaudio', 'ras' => 'image/x-cmu-raster', 'rdf' => 'application/rdf+xml', 'rgb' => 'image/x-rgb', 'rm' => 'application/vnd.rn-realmedia', 'roff' => 'application/x-troff', 'rtf' => 'text/rtf', 'rtx' => 'text/richtext', 'sgm' => 'text/sgml', 'sgml' => 'text/sgml', 'sh' => 'application/x-sh', 'shar' => 'application/x-shar', 'silo' => 'model/mesh', 'sit' => 'application/x-stuffit', 'skd' => 'application/x-koan', 'skm' => 'application/x-koan', 'skp' => 'application/x-koan', 'skt' => 'application/x-koan', 'smi' => 'application/smil', 'smil' => 'application/smil', 'snd' => 'audio/basic', 'so' => 'application/octet-stream', 'spl' => 'application/x-futuresplash', 'src' => 'application/x-wais-source', 'sv4cpio' => 'application/x-sv4cpio', 'sv4crc' => 'application/x-sv4crc', 'svg' => 'image/svg+xml', 'swf' => 'application/x-shockwave-flash', 't' => 'application/x-troff', 'tar' => 'application/x-tar', 'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex', 'texi' => 'application/x-texinfo', 'texinfo' => 'application/x-texinfo', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'tr' => 'application/x-troff', 'tsv' => 'text/tab-separated-values', 'txt' => 'text/plain', 'ustar' => 'application/x-ustar', 'vcd' => 'application/x-cdlink', 'vrml' => 'model/vrml', 'vxml' => 'application/voicexml+xml', 'wav' => 'audio/x-wav', 'wbmp' => 'image/vnd.wap.wbmp', 'wbxml' => 'application/vnd.wap.wbxml', 'webm' => 'video/webm', 'wml' => 'text/vnd.wap.wml', 'wmlc' => 'application/vnd.wap.wmlc', 'wmls' => 'text/vnd.wap.wmlscript', 'wmlsc' => 'application/vnd.wap.wmlscriptc', 'wmv' => 'video/x-ms-wmv', 'wrl' => 'model/vrml', 'xbm' => 'image/x-xbitmap', 'xht' => 'application/xhtml+xml', 'xhtml' => 'application/xhtml+xml', 'xls' => 'application/vnd.ms-excel', 'xml' => 'application/xml', 'xpm' => 'image/x-xpixmap', 'xsl' => 'application/xml', 'xslt' => 'application/xslt+xml', 'xul' => 'application/vnd.mozilla.xul+xml', 'xwd' => 'image/x-xwindowdump', 'xyz' => 'chemical/x-xyz', 'zip' => 'application/zip' ]; return isset($mime_types[$ext]) ? $mime_types[$ext] : 'application/octet-stream'; }
2022年06月21日
190 阅读
0 评论
0 点赞
2022-06-21
【PHP】好用的PHP函数集合-在线版
string函数:http://www.w3school.com.cn/php/php_ref_string.asp array函数:http://www.w3school.com.cn/php/php_ref_array.asp 日期函数:http://www.w3school.com.cn/php/php_ref_date.asp 日历函数:http://www.w3school.com.cn/php/php_ref_calendar.asp 目录函数:http://www.w3school.com.cn/php/php_ref_directory.asp 数学函数:http://php.net/manual/zh/ref.math.php
2022年06月21日
175 阅读
0 评论
0 点赞
2022-06-19
【PHP】PHP实现驼峰转下划线
function to_slash($array) { // 先转成json字符串,进行正则替换,再转换为数组 $tmp = json_encode($array); $tmp = strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', $tmp)); $tmp = json_decode($tmp, true); return $tmp; } 原理: // 驼峰转下划线 // 先添加分隔符,再转成小写 // userId => user_id $str = preg_replace('/((?<=[a-z])(?=[A-Z]))/','_',$str); $str = strtolower($str); // 下划线转驼峰 // 同样先添加分隔符,再转成大写 // user_id => userId $array = explode('_',$str); array_walk($array,create_function('&$v','$v=ucwords($v);')); $str = implode('',$array); // 首字母转小写 $str{0} = strtolower($str{0})
2022年06月19日
91 阅读
0 评论
0 点赞
2022-06-19
【PHP】PHP地理位置计算
<?php /** *求两个已知经纬度之间的距离,单位为米 *@param lng1,lng2 经度 *@param lat1,lat2 纬度 *@return float 距离,单位米 **/ function getdistance($lng1,$lat1,$lng2,$lat2){ //将角度转为狐度 $radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为弧度 $radLat2=deg2rad($lat2); $radLng1=deg2rad($lng1); $radLng2=deg2rad($lng2); $a=$radLat1-$radLat2; $b=$radLng1-$radLng2; $s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137*1000; return intval($s); } function distanceConvert($s){ $s = intval($s); if($s <= 0){ return '附近'; }elseif($s > 0 && $s <= 1000){ return $s.'m'; }elseif($s > 1000 && $s <= 2000){ return '<2km'; }elseif($s > 2000 && $s <= 3000){ return '<3km'; }elseif($s > 3000 && $s <= 4000){ return '<4km'; }elseif($s > 4000 && $s <= 5000){ return '<5km'; }elseif($s > 5000){ return '>5km'; } } /** * 根据条件获取商户列表 * @param string $lng * @param string $lat * @param string $city_id * @param string $district_id * @param string $type_id * @param string $search * @return mixed */ //如果存在经纬度则考虑范围5公里内数据,如果不存在经纬度,则不考虑 public function GetShopList($navigationId = 0,$page = 1,$size =10 ,$lng = '', $lat ='', $city_id = '',$district_id = '',$type_id = '',$search = '') { $field = 'id,province_id,city_id,district_id,navigation_id,type_id,name,phone,address,logo,stime,etime,rule,activity,discount,latitude,longitude,status,token,sort,description,gmt_created'; $where = array( 'is_deleted' => 0, 'is_show' => 1, 'navigation_id'=>$navigationId, ); //判断城市,县区,分类是否存在 if(!empty($city_id)){ $where['city_id|province_id'] = $city_id; } if(!empty($district_id)){ $where['district_id'] = $district_id; } if(!empty($type_id)){ $where['type_id'] = $type_id; } if(!empty($search)){ $where['name'] = array('like',"%{$search}%"); } $offset = ($page * $size) - ($size - 1); //如果存在经纬度,就根据经纬度查询5公里范围内数据 if(!empty($lng) && !empty($lat)) { // //求出半径 // $radius = 5;//半径范围,单位km // $rangeLat = 180 / pi() * $radius / 6372.797;//纬度范围 // $rangeLng = $rangeLat / cos($lat * pi() / 180.0); //经度范围 // $maxLat = $lat + $rangeLat; //x1 // $minLat = $lat - $rangeLat; //x0 // $maxLng = $lng + $rangeLng; //y1 // $minLng = $lng - $rangeLng; //y0 // // $where['latitude'] = array(array('lt',$maxLat),array('gt',$minLat)); // $where['longitude'] = array(array('lt',$maxLng),array('gt',$minLng)); $list = $this->where($where)->field($field)->order("abs(longitude - {$lng}) + abs(latitude - {$lat})")->limit($offset,$size)->select(); }else{ $list = $this->where($where)->field($field)->order('sort asc')->limit($offset,$size)->select(); } //记录错误日志 if ($list === false) { wLog('GetShopList error! sql:' . $this->getLastSql(), $this->errorFile); wLog('GetShopList error! errorInfo:' . $this->error, $this->errorFile); } //wLog('GetShopList sql:' . $this->getLastSql(), $this->errorFile); return $list; }
2022年06月19日
124 阅读
0 评论
0 点赞
2022-06-19
【PHP】PHP原生生成EXCEL
<?php /*** * 生成excle文件 ***/ $time = time(); $filename = date("Y年m月d日h点m分s秒", $time).'问卷数据'; $rows = Sp_Looks_Vote::downvote(); $file = $filename.".csv"; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); $tabletitle .= "购买地址,常常购买,喜欢模特,内容如何,印象如何,是否购买,感觉如何,购买方式,购买方式建议,吸引点,改进建议"; $conter = iconv('utf-8','gbk',$tabletitle)."\n"; echo $conter;die; /*** * 生成word文件 ***/ header("Content-Type: application/msword"); header("Content-Disposition: attachment; filename=doc.doc"); header("Pragma: no-cache"); header("Expires: 0"); $output = '<table border="1" cellspacing="2" cellpadding="2" width="90%" align="center">'; $output .= '<tr bgcolor="#cccccc"><td align="center">图片</td></tr>'; $output .= '<tr bgcolor="#f6f7fa"><td><span style="color:#FF0000;"><strong>下面是一张图片</strong></span& gt;</td></tr>'; $output .= '<tr><td align="center"><img src="http://zi.csdn.net/48260_2.gif"></td></tr>'; $output .= '</table>'; echo $output; ?>
2022年06月19日
189 阅读
0 评论
0 点赞
2022-06-19
【PHP】PHP的邮件发送类
<?php /** * 邮件发送类 * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件的邮件 * 需要的php扩展,sockets和Fileinfo。 * @example * $mail = new MySendMail(); * $mail->setServer("XXXXX", "XXXXX@XXXXX", "XXXXX"); 设置smtp服务器 * $mail->setFrom("XXXXX"); 设置发件人 * $mail->setReceiver("XXXXX"); 设置收件人,多个收件人,调用多次 * $mail->setCc("XXXX"); 设置抄送,多个抄送,调用多次 * $mail->setBcc("XXXXX"); 设置秘密抄送,多个秘密抄送,调用多次 * $mail->setMailInfo("test", "<b>test</b>"); 设置邮件主题、内容 * $mail->sendMail(); 发送 */ class MySendMail { /** * @var string 邮件传输代理用户名 * @access private */ private $_userName; /** * @var string 邮件传输代理密码 * @access private */ private $_password; /** * @var string 邮件传输代理服务器地址 * @access protected */ protected $_sendServer; /** * @var int 邮件传输代理服务器端口 * @access protected */ protected $_port=25; /** * @var string 发件人 * @access protected */ protected $_from; /** * @var string 收件人 * @access protected */ protected $_to; /** * @var string 抄送 * @access protected */ protected $_cc; /** * @var string 秘密抄送 * @access protected */ protected $_bcc; /** * @var string 主题 * @access protected */ protected $_subject; /** * @var string 邮件正文 * @access protected */ protected $_body; /** * @var string 附件 * @access protected */ protected $_attachment; /** * @var reource socket资源 * @access protected */ protected $_socket; /** * @var string 错误信息 * @access protected */ protected $_errorMessage; /** * 设置邮件传输代理,如果是可以匿名发送有邮件的服务器,只需传递代理服务器地址就行 * @access public * @param string $server 代理服务器的ip或者域名 * @param string $username 认证账号 * @param string $password 认证密码 * @param int $port 代理服务器的端口,smtp默认25号端口 * @return boolean */ public function setServer($server, $username="", $password="", $port=25) { $this->_sendServer = $server; $this->_port = $port; if(!empty($username)) { $this->_userName = base64_encode($username); } if(!empty($password)) { $this->_password = base64_encode($password); } return true; } /** * 设置发件人 * @access public * @param string $from 发件人地址 * @return boolean */ public function setFrom($from) { $this->_from = $from; return true; } /** * 设置收件人,多个收件人,连续调用多次. * @access public * @param string $to 收件人地址 * @return boolean */ public function setReceiver($to) { if(isset($this->_to)) { if(is_string($this->_to)) { $this->_to = array($this->_to); $this->_to[] = $to; return true; } elseif(is_array($this->_to)) { $this->_to[] = $to; return true; } else { return false; } } else { $this->_to = $to; return true; } } /** * 设置抄送,多个抄送,连续调用多次. * @access public * @param string $cc 抄送地址 * @return boolean */ public function setCc($cc) { if(isset($this->_cc)) { if(is_string($this->_cc)) { $this->_cc = array($this->_cc); $this->_cc[] = $cc; return true; } elseif(is_array($this->_cc)) { $this->_cc[] = $cc; return true; } else { return false; } } else { $this->_cc = $cc; return true; } } /** * 设置秘密抄送,多个秘密抄送,连续调用多次 * @access public * @param string $bcc 秘密抄送地址 * @return boolean */ public function setBcc($bcc) { if(isset($this->_bcc)) { if(is_string($this->_bcc)) { $this->_bcc = array($this->_bcc); $this->_bcc[] = $bcc; return true; } elseif(is_array($this->_bcc)) { $this->_bcc[] = $bcc; return true; } else { return false; } } else { $this->_bcc = $bcc; return true; } } /** * 设置邮件信息 * @access public * @param string $body 邮件主题 * @param string $subject 邮件主体内容,可以是纯文本,也可是是HTML文本 * @param string $attachment 附件,文件地址 * @return boolean */ public function setMailInfo($subject, $body, $attachment="") { $this->_subject = $subject; $this->_body = base64_encode($body); if(!empty($attachment)) { $this->_attachment = $attachment; } return true; } /** * 发送邮件 * @access public * @return boolean */ public function sendMail() { $command = $this->getCommand(); $this->socket(); foreach ($command as $value) { if($this->sendCommand($value[0], $value[1])) { continue; } else{ return false; } } //其实这里也没必要关闭,smtp命令:QUIT发出之后,服务器就关闭了连接,本地的socket资源会自动释放 $this->close(); echo 'Mail OK!'; return true; } /** * 返回错误信息 * @return string */ public function error(){ if(!isset($this->_errorMessage)) { $this->_errorMessage = ""; } return $this->_errorMessage; } /** * 返回mail命令 * @access protected * @return array */ protected function getCommand() { $command = array( array("HELO sendmail\r\n", 250) ); if(!empty($this->_userName)){ $command[] = array("AUTH LOGIN\r\n", 334); $command[] = array($this->_userName . "\r\n", 334); $command[] = array($this->_password . "\r\n", 235); } $command[] = array("MAIL FROM:<" . $this->_from . ">\r\n", 250); $separator = "----=_Part_" . md5($this->_from . time()) . uniqid(); //分隔符 //设置发件人 $header = "FROM: test<" . $this->_from . ">\r\n"; //设置收件人 if(is_array($this->_to)) { $count = count($this->_to); for($i=0; $i<$count; $i++){ $command[] = array("RCPT TO: <" . $this->_to[$i] . ">\r\n", 250); if($i == 0){ $header .= "TO: <" . $this->_to[$i] .">"; } elseif($i + 1 == $count){ $header .= ",<" . $this->_to[$i] .">\r\n"; } else{ $header .= ",<" . $this->_to[$i] .">"; } } } else{ $command[] = array("RCPT TO: <" . $this->_to . ">\r\n", 250); $header .= "TO: <" . $this->_to . ">\r\n"; } //设置抄送 if(isset($this->_cc)) { if(is_array($this->_cc)) { $count = count($this->_cc); for($i=0; $i<$count; $i++){ $command[] = array("RCPT TO: <" . $this->_cc[$i] . ">\r\n", 250); if($i == 0){ $header .= "CC: <" . $this->_cc[$i] .">"; } elseif($i + 1 == $count){ $header .= ",<" . $this->_cc[$i] .">\r\n"; } else{ $header .= ",<" . $this->_cc[$i] .">"; } } } else{ $command[] = array("RCPT TO: <" . $this->_cc . ">\r\n", 250); $header .= "CC: <" . $this->_cc . ">\r\n"; } } //设置秘密抄送 if(isset($this->_bcc)) { if(is_array($this->_bcc)) { $count = count($this->_bcc); for($i=0; $i<$count; $i++){ $command[] = array("RCPT TO: <" . $this->_bcc[$i] . ">\r\n", 250); if($i == 0){ $header .= "BCC: <" . $this->_bcc[$i] .">"; } elseif($i + 1 == $count){ $header .= ",<" . $this->_bcc[$i] .">\r\n"; } else{ $header .= ",<" . $this->_bcc[$i] .">"; } } } else{ $command[] = array("RCPT TO: <" . $this->_bcc . ">\r\n", 250); $header .= "BCC: <" . $this->_bcc . ">\r\n"; } } $header .= "Subject: " . $this->_subject ."\r\n"; if(isset($this->_attachment)) { //含有附件的邮件头需要声明成这个 $header .= "Content-Type: multipart/mixed;\r\n"; } elseif(false){ //邮件体含有图片资源的需要声明成这个 $header .= "Content-Type: multipart/related;\r\n"; } else{ //html或者纯文本的邮件声明成这个 $header .= "Content-Type: multipart/alternative;\r\n"; } //邮件头分隔符 $header .= "\t" . 'boundary="' . $separator . '"'; $header .= "\r\nMIME-Version: 1.0\r\n"; $header .= "\r\n--" . $separator . "\r\n"; $header .= "Content-Type:text/html; charset=utf-8\r\n"; $header .= "Content-Transfer-Encoding: base64\r\n\r\n"; $header .= $this->_body . "\r\n"; $header .= "--" . $separator . "\r\n"; //加入附件 if(isset($this->_attachment)){ $header .= "\r\n--" . $separator . "\r\n"; $header .= "Content-Type: " . $this->getMIMEType() . '; name="' . basename($this->_attachment) . '"' . "\r\n"; $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= 'Content-Disposition: attachment; filename="' . basename($this->_attachment) . '"' . "\r\n"; $header .= "\r\n"; $header .= $this->readFile(); $header .= "\r\n--" . $separator . "\r\n"; } $header .= "\r\n.\r\n"; $command[] = array("DATA\r\n", 354); $command[] = array($header, 250); $command[] = array("QUIT\r\n", 221); return $command; } /** * 发送命令 * @access protected * @param string $command 发送到服务器的smtp命令 * @param int $code 期望服务器返回的响应吗 * @return boolean */ protected function sendCommand($command, $code) { echo 'Send command:' . $command . ',expected code:' . $code . '<br />'; //发送命令给服务器 try{ if(socket_write($this->_socket, $command, strlen($command))){ //当邮件内容分多次发送时,没有$code,服务器没有返回 if(empty($code)) { return true; } //读取服务器返回 $data = trim(socket_read($this->_socket, 1024)); echo 'response:' . $data . '<br /><br />'; if($data) { $pattern = "/^".$code."/"; if(preg_match($pattern, $data)) { return true; } else{ $this->_errorMessage = "Error:" . $data . "|**| command:"; return false; } } else{ $this->_errorMessage = "Error:" . socket_strerror(socket_last_error()); return false; } } else{ $this->_errorMessage = "Error:" . socket_strerror(socket_last_error()); return false; } }catch(Exception $e) { $this->_errorMessage = "Error:" . $e->getMessage(); } } /** * 读取附件文件内容,返回base64编码后的文件内容 * @access protected * @return mixed */ protected function readFile() { if(isset($this->_attachment) && file_exists($this->_attachment)) { $file = file_get_contents($this->_attachment); return base64_encode($file); } else { return false; } } /** * 获取附件MIME类型 * @access protected * @return mixed */ protected function getMIMEType() { if(isset($this->_attachment) && file_exists($this->_attachment)) { $mime = mime_content_type($this->_attachment); if(! preg_match("/gif|jpg|png|jpeg/", $mime)){ $mime = "application/octet-stream"; } return $mime; } else { return false; } } /** * 建立到服务器的网络连接 * @access private * @return boolean */ private function socket() { if(!function_exists("socket_create")) { $this->_errorMessage = "Extension sockets must be enabled"; return false; } //创建socket资源 $this->_socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp')); if(!$this->_socket) { $this->_errorMessage = socket_strerror(socket_last_error()); return false; } socket_set_block($this->_socket);//设置阻塞模式 //连接服务器 if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) { $this->_errorMessage = socket_strerror(socket_last_error()); return false; } socket_read($this->_socket, 1024); return true; } /** * 关闭socket * @access private * @return boolean */ private function close() { if(isset($this->_socket) && is_object($this->_socket)) { $this->_socket->close(); return true; } $this->_errorMessage = "No resource can to be close"; return false; } } <?php /** * 邮件发送类 * 仅支持发送纯文本和HTML内容邮件 * 需要的php扩展,sockets * @example * $mail = new MySendMail(); * $mail->setServer("XXXXX", "XXXXX@XXXXX", "XXXXX"); 设置smtp服务器 * $mail->setFrom("XXXXX"); 设置发件人 * $mail->setReceiver("XXXXX"); 设置收件人 * $mail->setMailInfo("test", "<b>test</b>"); 设置邮件主题、内容 * $mail->sendMail(); 发送 */ class MySendMail2 { /** * @var string 邮件传输代理用户名 * @access private */ private $_userName; /** * @var string 邮件传输代理密码 * @access private */ private $_password; /** * @var string 邮件传输代理服务器地址 * @access private */ private $_sendServer; /** * @var int 邮件传输代理服务器端口 * @access protected */ protected $_port=25; /** * @var string 发件人 * @access protected */ protected $_from; /** * @var string 收件人 * @access protected */ protected $_to; /** * @var string 主题 * @access protected */ protected $_subject; /** * @var string 邮件正文 * @access protected */ protected $_body; /** * @var reource socket资源 * @access protected */ protected $_socket; /** * @var string 错误信息 * @access protected */ protected $_errorMessage; /** * 设置邮件传输代理,如果是可以匿名发送有邮件的服务器,只需传递代理服务器地址就行 * @access public * @param string $server 代理服务器的ip或者域名 * @param string $username 认证账号 * @param string $password 认证密码 * @param int $port 代理服务器的端口,smtp默认25号端口 * @return boolean */ public function setServer($server, $username="", $password="", $port=25) { $this->_sendServer = $server; $this->_port = $port; if(!empty($username)) { $this->_userName = base64_encode($username); } if(!empty($password)) { $this->_password = base64_encode($password); } return true; } /** * 设置发件人 * @access public * @param string $from 发件人地址 * @return boolean */ public function setFrom($from) { $this->_from = $from; return true; } /** * 设置收件人 * @access public * @param string $to 收件人地址 * @return boolean */ public function setReceiver($to) { $this->_to = $to; return true; } /** * 设置邮件信息 * @access public * @param string $body 邮件主题 * @param string $subject 邮件主体内容,可以是纯文本,也可是是HTML文本 * @return boolean */ public function setMailInfo($subject, $body) { $this->_subject = $subject; $this->_body = base64_encode($body); if(!empty($attachment)) { $this->_attachment = $attachment; } return true; } /** * 发送邮件 * @access public * @return boolean */ public function sendMail() { $command = $this->getCommand(); $this->socket(); foreach ($command as $value) { if($this->sendCommand($value[0], $value[1])) { continue; } else{ return false; } } //其实这里也没必要关闭,smtp命令:QUIT发出之后,服务器就关闭了连接,本地的socket资源会自动释放 $this->close(); echo 'Mail OK!'; return true; } /** * 返回错误信息 * @return string */ public function error(){ if(!isset($this->_errorMessage)) { $this->_errorMessage = ""; } return $this->_errorMessage; } /** * 返回mail命令 * @access protected * @return array */ protected function getCommand() { $separator = "----=_Part_" . md5($this->_from . time()) . uniqid(); //分隔符 $command = array( array("HELO sendmail\r\n", 250) ); if(!empty($this->_userName)){ $command[] = array("AUTH LOGIN\r\n", 334); $command[] = array($this->_userName . "\r\n", 334); $command[] = array($this->_password . "\r\n", 235); } //设置发件人 $command[] = array("MAIL FROM: <" . $this->_from . ">\r\n", 250); $header = "FROM: <" . $this->_from . ">\r\n"; //设置收件人 $command[] = array("RCPT TO: <" . $this->_to . ">\r\n", 250); $header .= "TO: <" . $this->_to . ">\r\n"; $header .= "Subject: " . $this->_subject ."\r\n"; $header .= "Content-Type: multipart/alternative;\r\n"; //邮件头分隔符 $header .= "\t" . 'boundary="' . $separator . '"'; $header .= "\r\nMIME-Version: 1.0\r\n"; $header .= "\r\n--" . $separator . "\r\n"; $header .= "Content-Type:text/html; charset=utf-8\r\n"; $header .= "Content-Transfer-Encoding: base64\r\n\r\n"; $header .= $this->_body . "\r\n"; $header .= "--" . $separator . "\r\n"; //结束数据 $header .= "\r\n.\r\n"; $command[] = array("DATA\r\n", 354); $command[] = array($header, 250); $command[] = array("QUIT\r\n", 221); return $command; } /** * 发送命令 * @access protected * @param string $command 发送到服务器的smtp命令 * @param int $code 期望服务器返回的响应吗 * @return boolean */ protected function sendCommand($command, $code) { echo 'Send command:' . $command . ',expected code:' . $code . '<br />'; //发送命令给服务器 try{ if(socket_write($this->_socket, $command, strlen($command))){ //当邮件内容分多次发送时,没有$code,服务器没有返回 if(empty($code)) { return true; } //读取服务器返回 $data = trim(socket_read($this->_socket, 1024)); echo 'response:' . $data . '<br /><br />'; if($data) { $pattern = "/^".$code."/"; if(preg_match($pattern, $data)) { return true; } else{ $this->_errorMessage = "Error:" . $data . "|**| command:"; return false; } } else{ $this->_errorMessage = "Error:" . socket_strerror(socket_last_error()); return false; } } else{ $this->_errorMessage = "Error:" . socket_strerror(socket_last_error()); return false; } }catch(Exception $e) { $this->_errorMessage = "Error:" . $e->getMessage(); } } /** * 建立到服务器的网络连接 * @access private * @return boolean */ private function socket() { if(!function_exists("socket_create")) { $this->_errorMessage = "Extension sockets must be enabled"; return false; } //创建socket资源 $this->_socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp')); if(!$this->_socket) { $this->_errorMessage = socket_strerror(socket_last_error()); return false; } socket_set_block($this->_socket);//设置阻塞模式 //连接服务器 if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) { $this->_errorMessage = socket_strerror(socket_last_error()); return false; } socket_read($this->_socket, 1024); return true; } /** * 关闭socket * @access private * @return boolean */ private function close() { if(isset($this->_socket) && is_object($this->_socket)) { $this->_socket->close(); return true; } $this->_errorMessage = "No resource can to be close"; return false; } } /**************************** Test ***********************************/ // $mail = new MySendMail(); // $mail->setServer("XXXX", "XXXXX@XXXXX", "XXXX"); // $mail->setFrom("XXXXX@XXXXX"); // $mail->setReceiver("XXXXX@XXXXX"); // $mail->setMailInfo("test", "<b>test</b>"); // $mail->sendMail(); ?>
2022年06月19日
168 阅读
0 评论
0 点赞
2022-06-19
【PHP】PHP获取汉字首个拼音
function getFirstChar($s0) { //ord() 函数返回字符串的首个字符的 ASCII 值 $fchar = ord($s0{0}); if ($fchar >= ord("A") and $fchar <= ord("z")) return strtoupper($s0{0}); $s1 = iconv("UTF-8", "gb2312", $s0); $s2 = iconv("gb2312", "UTF-8", $s1); if ($s2 == $s0) { $s = $s1; } else { $s = $s0; } $asc = ord($s{0}) * 256 + ord($s{1}) - 65536; if ($asc >= -20319 and $asc <= -20284) return "A"; if ($asc >= -20283 and $asc <= -19776) return "B"; if ($asc >= -19775 and $asc <= -19219) return "C"; if ($asc >= -19218 and $asc <= -18711) return "D"; if ($asc >= -18710 and $asc <= -18527) return "E"; if ($asc >= -18526 and $asc <= -18240) return "F"; if ($asc >= -18239 and $asc <= -17923) return "G"; if ($asc >= -17922 and $asc <= -17418) return "I"; if ($asc >= -17417 and $asc <= -16475) return "J"; if ($asc >= -16474 and $asc <= -16213) return "K"; if ($asc >= -16212 and $asc <= -15641) return "L"; if ($asc >= -15640 and $asc <= -15166) return "M"; if ($asc >= -15165 and $asc <= -14923) return "N"; if ($asc >= -14922 and $asc <= -14915) return "O"; if ($asc >= -14914 and $asc <= -14631) return "P"; if ($asc >= -14630 and $asc <= -14150) return "Q"; if ($asc >= -14149 and $asc <= -14091) return "R"; if ($asc >= -14090 and $asc <= -13319) return "S"; if ($asc >= -13318 and $asc <= -12839) return "T"; if ($asc >= -12838 and $asc <= -12557) return "W"; if ($asc >= -12556 and $asc <= -11848) return "X"; if ($asc >= -11847 and $asc <= -11056) return "Y"; if ($asc >= -11055 and $asc <= -10247) return "Z"; return null; } function chinesePhoneticize($zh) { $ret = ""; $s1 = iconv("UTF-8", "gb2312", $zh); $s2 = iconv("gb2312", "UTF-8", $s1); if ($s2 == $zh) { $zh = $s1; } for ($i = 0; $i < strlen($zh); $i++) { $s1 = substr($zh, $i, 1); $p = ord($s1); if ($p > 160) { $s2 = substr($zh, $i++, 2); $ret .= getfirstchar($s2); } else { $ret .= $s1; } } return $ret; } echo "这是中文字符串<br/>"; echo pinyin1('这是中文字符串');
2022年06月19日
209 阅读
0 评论
0 点赞
2022-06-19
【PHP】PHP根据身份证号获取星座
<?php // 根据身份证号,自动返回对应的星座 function get_xingzuo($cid) { $cid = getIDCard($cid); if (!isIdCard($cid)) return ''; $bir = substr($cid,10,4); $month = (int)substr($bir,0,2); $day = (int)substr($bir,2); $strValue = ''; if (($month == 1 && $day >= 20) || ($month == 2 && $day <= 18)) { $strValue = "水瓶座"; } else if (($month == 2 && $day >= 19) || ($month == 3 && $day <= 20)) { $strValue = "双鱼座"; } else if (($month == 3 && $day > 20) || ($month == 4 && $day <= 19)) { $strValue = "白羊座"; } else if (($month == 4 && $day >= 20) || ($month == 5 && $day <= 20)) { $strValue = "金牛座"; } else if (($month == 5 && $day >= 21) || ($month == 6 && $day <= 21)) { $strValue = "双子座"; } else if (($month == 6 && $day > 21) || ($month == 7 && $day <= 22)) { $strValue = "巨蟹座"; } else if (($month == 7 && $day > 22) || ($month == 8 && $day <= 22)) { $strValue = "狮子座"; } else if (($month == 8 && $day >= 23) || ($month == 9 && $day <= 22)) { $strValue = "处女座"; } else if (($month == 9 && $day >= 23) || ($month == 10 && $day <= 23)) { $strValue = "天秤座"; } else if (($month == 10 && $day > 23) || ($month == 11 && $day <= 22)) { $strValue = "天蝎座"; } else if (($month == 11 && $day > 22) || ($month == 12 && $day <= 21)) { $strValue = "射手座"; } else if (($month == 12 && $day > 21) || ($month == 1 && $day <= 19)) { $strValue = "魔羯座"; } return $strValue; } // 根据身份证号,自动返回对应的生肖 function get_shengxiao($cid) { $cid = getIDCard($cid); if (!isIdCard($cid)) return ''; $start = 1901; $end = $end = (int)substr($cid,6,4); $x = ($start - $end) % 12; $value = ""; if ($x == 1 || $x == -11) {$value = "鼠";} if ($x == 0) { $value = "牛";} if ($x == 11 || $x == -1) {$value = "虎";} if ($x == 10 || $x == -2) {$value = "兔";} if ($x == 9 || $x == -3) {$value = "龙";} if ($x == 8 || $x == -4) {$value = "蛇";} if ($x == 7 || $x == -5) {$value = "马";} if ($x == 6 || $x == -6) {$value = "羊";} if ($x == 5 || $x == -7) {$value = "猴";} if ($x == 4 || $x == -8) {$value = "鸡";} if ($x == 3 || $x == -9) {$value = "狗";} if ($x == 2 || $x == -10) {$value = "猪";} return $value; } function get_xingbie($cid,$comm=0) { //根据身份证号,自动返回性别 $cid = getIDCard($cid); if (!isIdCard($cid)) return ''; $sexint = (int)substr($cid,16,1); if($comm >0){ return $sexint % 2 === 0 ? '女士' : '先生'; }else{ return $sexint % 2 === 0 ? '女' : '男'; } } // 功能:把15位身份证转换成18位 function getIDCard($idCard) { // 若是15位,则转换成18位;否则直接返回ID if (15 == strlen ( $idCard )) { $W = array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1 ); $A = array ("1","0","X","9","8","7","6","5","4","3","2" ); $s = 0; $idCard18 = substr ( $idCard, 0, 6 ) . "19" . substr ( $idCard, 6 ); $idCard18_len = strlen ( $idCard18 ); for($i = 0; $i < $idCard18_len; $i ++) { $s = $s + substr ( $idCard18, $i, 1 ) * $W [$i]; } $idCard18 .= $A [$s % 11]; return $idCard18; } else { return $idCard; } } ?>
2022年06月19日
157 阅读
0 评论
0 点赞
2022-06-19
【PHP】数据校验规则
/** * @name: check_data * @description: 检测数据规则 * @param: string 被检测的原字符串 * @param: string 被检测的类型 * @return: boolean * @create: 2014-10-10 **/ function check_data($string, $type='email'){ $return = FALSE; switch($type){ case 'email' : {$return = preg_match("/^(\w+[-+.]*\w+)*@(\w+([-.]*\w+)*\.\w+([-.]*\w+)*)$/", $string); break;} case 'http' : {$return = preg_match("/^http:\/\/[A-Za-z0-9-]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"])*$/", $string); break;} case 'qq' : {$return = preg_match("/^[1-9]\d{4,11}$/", $string); break;} case 'post' : {$return = preg_match("/^[1-9]\d{5}$/", $string); break;} case 'idnum' : {$return = preg_match("/^\d{15}(\d{2}[A-Za-z0-9])?$/", $string); break;} case 'china' : {$return = preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $string); break;} //GBK中文 case 'english' : {$return = preg_match("/^[A-Za-z]+$/", $string); break;} case 'mobile' : {$return = preg_match("/^((\(\d{3}\))|(\d{3}\-))?((13)|(14)|(15)|(17)|(18)){1}\d{9}$/", $string); break;} case 'phone' : {$return = preg_match("/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/", $string); break;} case 'safe' : {$return = preg_match("/^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/", $string) != 0 ? TRUE : FALSE; break;} case 'age' : {$return = (preg_match("/^(-{0,1}|\+{0,1})[0-9]+(\.{0,1}[0-9]+)$/", $string) && intval($string) <= 130 && intval($string) >= 12) ? TRUE : FALSE; break;} case 'eng_num' : {$return = preg_match("/^[A-Za-z0-9]+$/", $string); break;} case 'password' : {$return = (preg_match("/^[A-Za-z0-9]+$/", $string) && strlen($string) <= 32 && strlen($string) >= 6) ? TRUE : FALSE; break;} case 'datetime' : {$return = preg_match('/^[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}$/', $string); break;} case 'datetimes' : {$return = preg_match('/^[\d]{4}-[\d]{2}-[\d]{2}\s[\d]{2}:[\d]{2}:[\d]{2}$/', $string); break;} case 'date' : {$return = preg_match('/^[\d]{4}-[\d]{1,2}-[\d]{1,2}$/', $string); break;} case 'dates' : {$return = preg_match('/^[\d]{4}-[\d]{2}-[\d]{2}$/', $string); break;} case 'time' : {$return = preg_match('/^[\d]{1,2}:[\d]{1,2}:[\d]{1,2}$/', $string); break;} case 'times' : {$return = preg_match('/^[\d]{2}:[\d]{2}:[\d]{2}$/', $string); break;} case 'ip' : {$return = preg_match("/^\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b$/", $string); break;} case 'incchinese' : {$return = preg_match('/[\x{4e00}-\x{9fa5}]+/u', $string); break;} //是否包含中文 case 'plusnum' : {$return = preg_match('/^[1-9]*[1-9][0-9]*$/', $string); break;} //是否是正整数 case 'hostrecord' : {$return = preg_match('/^[A-Z_a-z0-9][A-Za-z0-9-]+(\.[A-Za-z0-9-_]+)*$/', $string); break;} //正确的主机记录,english case 'cnhostrecord' : {$return = preg_match('/^[_a-zA-Z0-9]*([\x{4e00}-\x{9fa5}]*[-a-zA-Z0-9\.]*)+[a-zA-Z0-9_]$/iu', $string); break;} //正确的主机记录,english chinese case 'domain' : {$return = preg_match('/^[A-Za-z0-9][A-Za-z0-9-]+(\.[A-Za-z0-9-]+){1,3}$/', $string); break;} //是否是域名 case 'cndomain' : {$return = (preg_match('/[\x{4e00}-\x{9fa5}]+/u', $string) && preg_match('/^([-a-zA-Z0-9\.]*[\x{4e00}-\x{9fa5}]*[-a-zA-Z0-9\.]*)+\.(中国|公司|网络|CN|COM|NET)$/iu', $string)) ? TRUE : FALSE; break;} //是否中文域名 case 'mac' : {$return = preg_match('/^[a-fA-F\d]{2}:[a-fA-F\d]{2}:[a-fA-F\d]{2}:[a-fA-F\d]{2}:[a-fA-F\d]{2}:[a-fA-F\d]{2}$/', $string); break;} case 'ipv6' : {$return = preg_match('/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/', $string); break;} } gettype($return) == 'integer' && $return = $return == 0 ? FALSE : TRUE; return $return; }
2022年06月19日
184 阅读
0 评论
0 点赞
2022-06-19
【PHP】PHP检测浏览器语言
如果你的网站上有多种语言,那么可以使用这段代码 作为默认的语言来检测浏览器语言。该段代码将返回 浏览器客户端使用的初始语言。 function get_client_language($availableLanguages, $default='en'){ if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']); foreach ($langs as $value){ $choice=substr($value,0,2); if(in_array($choice, $availableLanguages)){ return $choice; } } } return $default; }
2022年06月19日
128 阅读
0 评论
0 点赞
2022-06-19
【PHP】PHP实现字符串截取,支持中文和其他编码
<?php /** * 字符串截取,支持中文和其他编码 * @param [string] $str [字符串] * @param integer $start [起始位置] * @param integer $length [截取长度] * @param string $charset [字符串编码] * @param boolean $suffix [是否有省略号] * @return [type] [description] */ function msubstr($str, $start=0, $length=15, $charset="utf-8", $suffix=true) { if(function_exists("mb_substr")) { return mb_substr($str, $start, $length, $charset); } elseif(function_exists('iconv_substr')) { return iconv_substr($str,$start,$length,$charset); } $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; preg_match_all($re[$charset], $str, $match); $slice = join("",array_slice($match[0], $start, $length)); if($suffix) { return $slice."…"; } return $slice; } /* 这里的关键,$charset="utf-8",对中文支持是很重要的!不然出现一些?号,就要挨批了! 使用方法如下: echo msubstr($str, $start=0, $length=15, $charset="utf-8", $suffix=true) /*
2022年06月19日
170 阅读
0 评论
0 点赞
2022-06-19
【PHP】PHP实现中文转拼音
<?php /** * PHP 汉字转拼音 * @author Jerryli(hzjerry@gmail.com) * @version V0.20140715 * @package SPFW.core.lib.final * @global SEA_PHP_FW_VAR_ENV * @example * echo CUtf8_PY::encode('阿里巴巴科技有限公司'); //编码为拼音首字母 * echo CUtf8_PY::encode('阿里巴巴科技有限公司', 'all'); //编码为全拼音 */ class CUtf8_PY { /** * 拼音字符转换图 * @var array */ private static $_aMaps = array( 'a'=>-20319,'ai'=>-20317,'an'=>-20304,'ang'=>-20295,'ao'=>-20292, 'ba'=>-20283,'bai'=>-20265,'ban'=>-20257,'bang'=>-20242,'bao'=>-20230,'bei'=>-20051,'ben'=>-20036,'beng'=>-20032,'bi'=>-20026,'bian'=>-20002,'biao'=>-19990,'bie'=>-19986,'bin'=>-19982,'bing'=>-19976,'bo'=>-19805,'bu'=>-19784, 'ca'=>-19775,'cai'=>-19774,'can'=>-19763,'cang'=>-19756,'cao'=>-19751,'ce'=>-19746,'ceng'=>-19741,'cha'=>-19739,'chai'=>-19728,'chan'=>-19725,'chang'=>-19715,'chao'=>-19540,'che'=>-19531,'chen'=>-19525,'cheng'=>-19515,'chi'=>-19500,'chong'=>-19484,'chou'=>-19479,'chu'=>-19467,'chuai'=>-19289,'chuan'=>-19288,'chuang'=>-19281,'chui'=>-19275,'chun'=>-19270,'chuo'=>-19263,'ci'=>-19261,'cong'=>-19249,'cou'=>-19243,'cu'=>-19242,'cuan'=>-19238,'cui'=>-19235,'cun'=>-19227,'cuo'=>-19224, 'da'=>-19218,'dai'=>-19212,'dan'=>-19038,'dang'=>-19023,'dao'=>-19018,'de'=>-19006,'deng'=>-19003,'di'=>-18996,'dian'=>-18977,'diao'=>-18961,'die'=>-18952,'ding'=>-18783,'diu'=>-18774,'dong'=>-18773,'dou'=>-18763,'du'=>-18756,'duan'=>-18741,'dui'=>-18735,'dun'=>-18731,'duo'=>-18722, 'e'=>-18710,'en'=>-18697,'er'=>-18696, 'fa'=>-18526,'fan'=>-18518,'fang'=>-18501,'fei'=>-18490,'fen'=>-18478,'feng'=>-18463,'fo'=>-18448,'fou'=>-18447,'fu'=>-18446, 'ga'=>-18239,'gai'=>-18237,'gan'=>-18231,'gang'=>-18220,'gao'=>-18211,'ge'=>-18201,'gei'=>-18184,'gen'=>-18183,'geng'=>-18181,'gong'=>-18012,'gou'=>-17997,'gu'=>-17988,'gua'=>-17970,'guai'=>-17964,'guan'=>-17961,'guang'=>-17950,'gui'=>-17947,'gun'=>-17931,'guo'=>-17928, 'ha'=>-17922,'hai'=>-17759,'han'=>-17752,'hang'=>-17733,'hao'=>-17730,'he'=>-17721,'hei'=>-17703,'hen'=>-17701,'heng'=>-17697,'hong'=>-17692,'hou'=>-17683,'hu'=>-17676,'hua'=>-17496,'huai'=>-17487,'huan'=>-17482,'huang'=>-17468,'hui'=>-17454,'hun'=>-17433,'huo'=>-17427, 'ji'=>-17417,'jia'=>-17202,'jian'=>-17185,'jiang'=>-16983,'jiao'=>-16970,'jie'=>-16942,'jin'=>-16915,'jing'=>-16733,'jiong'=>-16708,'jiu'=>-16706,'ju'=>-16689,'juan'=>-16664,'jue'=>-16657,'jun'=>-16647, 'ka'=>-16474,'kai'=>-16470,'kan'=>-16465,'kang'=>-16459,'kao'=>-16452,'ke'=>-16448,'ken'=>-16433,'keng'=>-16429,'kong'=>-16427,'kou'=>-16423,'ku'=>-16419,'kua'=>-16412,'kuai'=>-16407,'kuan'=>-16403,'kuang'=>-16401,'kui'=>-16393,'kun'=>-16220,'kuo'=>-16216, 'la'=>-16212,'lai'=>-16205,'lan'=>-16202,'lang'=>-16187,'lao'=>-16180,'le'=>-16171,'lei'=>-16169,'leng'=>-16158,'li'=>-16155,'lia'=>-15959,'lian'=>-15958,'liang'=>-15944,'liao'=>-15933,'lie'=>-15920,'lin'=>-15915,'ling'=>-15903,'liu'=>-15889,'long'=>-15878,'lou'=>-15707,'lu'=>-15701,'lv'=>-15681,'luan'=>-15667,'lue'=>-15661,'lun'=>-15659,'luo'=>-15652, 'ma'=>-15640,'mai'=>-15631,'man'=>-15625,'mang'=>-15454,'mao'=>-15448,'me'=>-15436,'mei'=>-15435,'men'=>-15419,'meng'=>-15416,'mi'=>-15408,'mian'=>-15394,'miao'=>-15385,'mie'=>-15377,'min'=>-15375,'ming'=>-15369,'miu'=>-15363,'mo'=>-15362,'mou'=>-15183,'mu'=>-15180, 'na'=>-15165,'nai'=>-15158,'nan'=>-15153,'nang'=>-15150,'nao'=>-15149,'ne'=>-15144,'nei'=>-15143,'nen'=>-15141,'neng'=>-15140,'ni'=>-15139,'nian'=>-15128,'niang'=>-15121,'niao'=>-15119,'nie'=>-15117,'nin'=>-15110,'ning'=>-15109,'niu'=>-14941,'nong'=>-14937,'nu'=>-14933,'nv'=>-14930,'nuan'=>-14929,'nue'=>-14928,'nuo'=>-14926, 'o'=>-14922,'ou'=>-14921, 'pa'=>-14914,'pai'=>-14908,'pan'=>-14902,'pang'=>-14894,'pao'=>-14889,'pei'=>-14882,'pen'=>-14873,'peng'=>-14871,'pi'=>-14857,'pian'=>-14678,'piao'=>-14674,'pie'=>-14670,'pin'=>-14668,'ping'=>-14663,'po'=>-14654,'pu'=>-14645, 'qi'=>-14630,'qia'=>-14594,'qian'=>-14429,'qiang'=>-14407,'qiao'=>-14399,'qie'=>-14384,'qin'=>-14379,'qing'=>-14368,'qiong'=>-14355,'qiu'=>-14353,'qu'=>-14345,'quan'=>-14170,'que'=>-14159,'qun'=>-14151, 'ran'=>-14149,'rang'=>-14145,'rao'=>-14140,'re'=>-14137,'ren'=>-14135,'reng'=>-14125,'ri'=>-14123,'rong'=>-14122,'rou'=>-14112,'ru'=>-14109,'ruan'=>-14099,'rui'=>-14097,'run'=>-14094,'ruo'=>-14092, 'sa'=>-14090,'sai'=>-14087,'san'=>-14083,'sang'=>-13917,'sao'=>-13914,'se'=>-13910,'sen'=>-13907,'seng'=>-13906,'sha'=>-13905,'shai'=>-13896,'shan'=>-13894,'shang'=>-13878,'shao'=>-13870,'she'=>-13859,'shen'=>-13847,'sheng'=>-13831,'shi'=>-13658,'shou'=>-13611,'shu'=>-13601,'shua'=>-13406,'shuai'=>-13404,'shuan'=>-13400,'shuang'=>-13398,'shui'=>-13395,'shun'=>-13391,'shuo'=>-13387,'si'=>-13383,'song'=>-13367,'sou'=>-13359,'su'=>-13356,'suan'=>-13343,'sui'=>-13340,'sun'=>-13329,'suo'=>-13326, 'ta'=>-13318,'tai'=>-13147,'tan'=>-13138,'tang'=>-13120,'tao'=>-13107,'te'=>-13096,'teng'=>-13095,'ti'=>-13091,'tian'=>-13076,'tiao'=>-13068,'tie'=>-13063,'ting'=>-13060,'tong'=>-12888,'tou'=>-12875,'tu'=>-12871,'tuan'=>-12860,'tui'=>-12858,'tun'=>-12852,'tuo'=>-12849, 'wa'=>-12838,'wai'=>-12831,'wan'=>-12829,'wang'=>-12812,'wei'=>-12802,'wen'=>-12607,'weng'=>-12597,'wo'=>-12594,'wu'=>-12585, 'xi'=>-12556,'xia'=>-12359,'xian'=>-12346,'xiang'=>-12320,'xiao'=>-12300,'xie'=>-12120,'xin'=>-12099,'xing'=>-12089,'xiong'=>-12074,'xiu'=>-12067,'xu'=>-12058,'xuan'=>-12039,'xue'=>-11867,'xun'=>-11861, 'ya'=>-11847,'yan'=>-11831,'yang'=>-11798,'yao'=>-11781,'ye'=>-11604,'yi'=>-11589,'yin'=>-11536,'ying'=>-11358,'yo'=>-11340,'yong'=>-11339,'you'=>-11324,'yu'=>-11303,'yuan'=>-11097,'yue'=>-11077,'yun'=>-11067, 'za'=>-11055,'zai'=>-11052,'zan'=>-11045,'zang'=>-11041,'zao'=>-11038,'ze'=>-11024,'zei'=>-11020,'zen'=>-11019,'zeng'=>-11018,'zha'=>-11014,'zhai'=>-10838,'zhan'=>-10832,'zhang'=>-10815,'zhao'=>-10800,'zhe'=>-10790,'zhen'=>-10780,'zheng'=>-10764,'zhi'=>-10587,'zhong'=>-10544,'zhou'=>-10533,'zhu'=>-10519,'zhua'=>-10331,'zhuai'=>-10329,'zhuan'=>-10328,'zhuang'=>-10322,'zhui'=>-10315,'zhun'=>-10309,'zhuo'=>-10307,'zi'=>-10296,'zong'=>-10281,'zou'=>-10274,'zu'=>-10270,'zuan'=>-10262,'zui'=>-10260,'zun'=>-10256,'zuo'=>-10254 ); /** * 将中文编码成拼音 * @param string $utf8Data utf8字符集数据 * @param string $sRetFormat 返回格式 [head:首字母|all:全拼音] * @return string */ public static function encode($utf8Data, $sRetFormat='head'){ $sGBK = iconv('UTF-8', 'GBK', $utf8Data); $aBuf = array(); for ($i=0, $iLoop=strlen($sGBK); $i<$iLoop; $i++) { $iChr = ord($sGBK{$i}); if ($iChr>160) $iChr = ($iChr<<8) + ord($sGBK{++$i}) - 65536; if ('head' === $sRetFormat) $aBuf[] = substr(self::zh2py($iChr),0,1); else $aBuf[] = self::zh2py($iChr); } if ('head' === $sRetFormat) return implode('', $aBuf); else return implode(' ', $aBuf); } /** * 中文转换到拼音(每次处理一个字符) * @param number $iWORD 待处理字符双字节 * @return string 拼音 */ private static function zh2py($iWORD) { if($iWORD>0 && $iWORD<160 ) { return chr($iWORD); } elseif ($iWORD<-20319||$iWORD>-10247) { return ''; } else { foreach (self::$_aMaps as $py => $code) { if($code > $iWORD) break; $result = $py; } return $result; } } } ?>
2022年06月19日
155 阅读
0 评论
0 点赞
2022-06-19
【PHP】PHP身份证验证类
<?php /** *身份证号处理函数 */ /** * 功能:根据身份证号,自动返回生日 * @param stirng $IDCard 身份证号 * @return Ambigous <string, NULL> */ function getBrithday ($idCard) { if (strlen($idCard) == 18) { $birthday = substr($idCard, 6, 4) . '-' . substr($idCard, 10, 2) . '-' . substr($idCard, 12, 2); } elseif (strlen($idCard) == 15) { $birthday = "19" . substr($idCard, 6, 2) . '-' . substr($idCard, 8, 2) . '-' . substr($idCard, 10, 2); } else { $birthday = null; } return $birthday; } /** * 功能:根据身份证号,自动返回性别 * @param string $cid 身份证号 * @param number $comm 返回的性别类型 * @return string 男/女 1/0 */ function getSex ($cid, $comm = 0) { $cid = getIDCard($cid); $sexint = (int) substr($cid, 16, 1); if ($comm > 0) { return $sexint % 2 === 0 ? '女' : '男'; } else { return $sexint % 2 === 0 ? '0' : '1'; } } /** * 功能:把15位身份证转换成18位 * @param string $idCard * @return newid or id */ function getIDCard ($idCard) { // 若是15位,则转换成18位;否则直接返回ID if (15 == strlen($idCard)) { $w = array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1); $a = array("1","0","X","9","8","7","6","5","4","3","2"); $s = 0; $idCard18 = substr($idCard, 0, 6) . "19" . substr($idCard, 6); $idCard18_len = strlen($idCard18); for ($i = 0; $i < $idCard18_len; $i ++) { $s = $s + substr($idCard18, $i, 1) * $w[$i]; } return $idCard18.$a[$s % 11]; } else { return $idCard; } } /** * 根据身份证号返回年龄 * @param string $id 身份证号 * @return string|number */ function getAge($id){ //过了这年的生日才算多了1周岁 if(empty($id)) return ''; $date=strtotime(substr($id,6,8)); //获得出生年月日的时间戳 $today=strtotime('today'); //获得今日的时间戳 $diff=floor(($today-$date)/86400/365); //得到两个日期相差的大体年数 //strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比 $age=strtotime(substr($id,6,8).' +'.$diff.'years')>$today?($diff+1):$diff; return $age; }
2022年06月19日
203 阅读
0 评论
0 点赞
2022-06-19
【PHP】PHP生成唯一ID
<?php header("Content-Type: text/html; charset=UTF-8"); date_default_timezone_set("PRC"); // error_reporting(0); // 生成ID:Unix时间戳+毫秒+微妙+四位随机数字(20位数字,2028/06/15 前可存入MySQL,数据类型:BIGINT) $id = microtime(); $id = substr($id, 11) . substr($id, 2, 6) . mt_rand(1000, 9999); //循环里面可用 public function setUUID(){ $currentTimestamp = microtime(true); $uuid = self::generateUUIDInner(); while (in_array($uuid, self::$generatedUUIDs)) { $uuid = self::generateUUIDInner(); } self::$generatedUUIDs[] = $uuid; self::$lastTimestamp = $currentTimestamp; return $uuid; } private static function generateUUIDInner() { $milliseconds = floor(microtime(true) * 1000); $randomPart = mt_rand(100, 999); $uuid = date('YmdHis').substr(strval($milliseconds), -3).$randomPart; return $uuid; echo "Generated UUID inside: ".$uuid."\n"; die; }
2022年06月19日
193 阅读
0 评论
0 点赞
2022-06-19
【PHP】PHP接口返回数据格式
/** * @Author: 小破孩嫩 * @Email: 3584685883@qq.com * @Time: 2020/9/30 16:35 * @param $code 状态码 * @param string $msg 提示信息 * @param array $data 数据 * @Description:返回标格式 */ function returnResponse ($code,$msg = '',$data=array()){ $retData = array( 'code' => $code, 'message' => $msg, 'data' => $data?$data:'', ); #错误时不显示data if($code != 200){ unset($retData['data']); } header('Content-Type:application/json; charset=utf-8'); exit(json_encode($retData,JSON_UNESCAPED_UNICODE)); } public function show($code = '', $message = '', $result = '', $httpCode = 200){ $data = [ 'code' => $code, 'message' => $message, 'result' => $result, ]; if(empty($data['result'])){ unset($data['result']); } return json($data,$httpCode); }这个方法只是code值在200的时候才会有返回data;
2022年06月19日
122 阅读
0 评论
0 点赞
2022-06-17
【PHP】PHP函数大全
PHP函数大全有道云地址:http://note.youdao.com/noteshare?id=f1271100f4366b0338e9a45b8bbd4db7&sub=0DA577EC658B4923A67FA4EB862D2FDA
2022年06月17日
141 阅读
0 评论
0 点赞
2022-06-17
【PHP】TP5.1框架获取服务器信息
//获取服务器相关信息 $info = [ '操作系统'=>PHP_OS, '运行环境'=>$_SERVER["SERVER_SOFTWARE"], 'PHP运行方式'=>php_sapi_name(), 'ThinkPHP版本'=> 'V'. \think\facade\App::version(), '上传附件限制'=>ini_get('upload_max_filesize'), '执行时间限制'=>ini_get('max_execution_time').'秒', '服务器时间'=>date("Y年n月j日 H:i:s"), '北京时间'=>gmdate("Y年n月j日 H:i:s",time()+8*3600), '服务器所处时区' => date_default_timezone_get(), '服务器域名/IP'=>$_SERVER['SERVER_NAME'].' [ '.gethostbyname($_SERVER['SERVER_NAME']).' ]', '剩余空间'=>round((disk_free_space(".")/(1024*1024)),2).'M', 'register_globals'=>get_cfg_var("register_globals")=="1" ? "ON" : "OFF", 'magic_quotes_gpc'=>(1===get_magic_quotes_gpc())?'YES':'NO', 'magic_quotes_runtime'=>(1===get_magic_quotes_runtime())?'YES':'NO' ]; /** * 获取当前的运行环境 * @return string */ function getHuanjing(){ switch (PHP_SAPI){ case 'fpm-fcgi': return 'nginx+php-fpm'; break; case 'cgi-fcgi': return 'nginx+fastcgi'; break; case 'apache2handler': return 'apache'; break; default: return PHP_SAPI; } } /** * 获取数据库版本 */ function dbVersion(){ $db = think\Db::query('select VERSION()'); return $db[0]['VERSION()']; } /** * 获取最大上传文件大小 */ function get_upload_max_filesize(){ return ini_get('upload_max_filesize'); }
2022年06月17日
288 阅读
1 评论
0 点赞
2022-06-17
【PHP】PHP实现阿拉伯数字转化为大写汉字
class Num2Cny{ static $basical = array(0 => "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); static $advanced = array(1 => "拾", "佰", "仟"); public static function ParseNumber($number){ $number = trim($number); if ($number > 999999999999) return "数字太大,无法处理。抱歉!"; if ($number == 0) return "零"; if (strpos($number, '.')) { $number = round($number, 2); $data = explode(".", $number); $data[0] = self::int($data[0]); $data[1] = self::dec($data[1]); return $data[0] . $data[1]; } else { return self::int($number) . '整'; } } public static function int($number){ $arr = array_reverse(str_split($number)); $data = ''; $zero = false; $zero_num = 0; foreach ($arr as $k => $v) { $_chinese = ''; $zero = ($v == 0) ? true : false; $x = $k % 4; if ($x && $zero && $zero_num > 1) continue; switch ($x) { case 0: if ($zero) { $zero_num = 0; } else { $_chinese = self::$basical[$v]; $zero_num = 1; } if ($k == 8) { $_chinese .= '亿'; } elseif ($k == 4) { $_chinese .= '万'; } break; default: if ($zero) { if ($zero_num == 1) { $_chinese = self::$basical[$v]; $zero_num++; } } else { $_chinese = self::$basical[$v]; $_chinese .= self::$advanced[$x]; } } $data = $_chinese . $data; } return $data . '元'; } public static function dec($number){ if (strlen($number) < 2) $number .= '0'; $arr = array_reverse(str_split($number)); $data = ''; $zero_num = false; foreach ($arr as $k => $v) { $zero = ($v == 0) ? true : false; $_chinese = ''; if ($k == 0) { if (!$zero) { $_chinese = self::$basical[$v]; $_chinese .= '分'; $zero_num = true; } } else { if ($zero) { if ($zero_num) { $_chinese = self::$basical[$v]; } } else { $_chinese = self::$basical[$v]; $_chinese .= '角'; } } $data = $_chinese . $data; } return $data; } } 使用方法 Num2Cny::ParseNumber($number); 注意:命名空间,访问方法,接受的$number数据验证
2022年06月17日
205 阅读
0 评论
0 点赞
2022-06-17
【PHP】PHP实现表格转换成数组
/** * @Author:小破孩 * @Time:2020/07/20 11:42 * @param $table 表格html代码 * @return mixed * @Description: 表格转数组 */ function tableArr($table){ $table = preg_replace("'<table[^>]*?>'si", "", $table); $table = preg_replace("'<tr[^>]*?>'si", "", $table); $table = preg_replace("'<td[^>]*?>'si", "", $table); $table = str_replace("</tr>", "{tr}", $table); $table = str_replace("</td>", "{td}", $table); //去掉 HTML 标记 $table = preg_replace("'<[/!]*?[^<>]*?>'si", "", $table); //去掉空白字符 $table = preg_replace("'([rn])[s]+'", "", $table); $table = preg_replace('/ /', "", $table); $table = str_replace(" ", "", $table); $table = str_replace(" ", "", $table); $table = explode('{tr}', $table); array_pop($table); foreach ($table as $key => $tr) { $td = explode('{td}', $tr); array_pop($td); $td_array[] = $td; } return $td_array; }
2022年06月17日
260 阅读
0 评论
0 点赞
1
2
3
4