首页
关于
归档
朋友
留言
API平台
告白墙
休闲游戏
更多
留言板
练字贴
Layui手册
Search
1
【PHP】PHPoffice/PHPSpreadsheet读取和写入Excel
1,006 阅读
2
【Git】No tracked branch configured for branch master or the branch doesn't exist.
742 阅读
3
【composer】composer常用命令
487 阅读
4
【Layui】控制页面元素展示隐藏
437 阅读
5
【MySQL】MySQL触发器应用场景和使用方法
417 阅读
默认分类
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
基础
小破孩
累计撰写
212
篇文章
累计收到
16
条评论
首页
栏目
默认分类
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手册
搜索到
51
篇与
的结果
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日
112 阅读
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日
133 阅读
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日
102 阅读
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日
129 阅读
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日
96 阅读
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日
105 阅读
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日
116 阅读
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日
145 阅读
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日
195 阅读
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日
139 阅读
0 评论
0 点赞
2022-06-17
【PHP】PHP多维数组转一维数组
function multi2array($array) { static $result_array = array(); foreach ($array as $key => $value) { if (is_array($value)) { multi2array($value); } else{ $result_array[] = $value; } } return $result_array; } function _arr($array,$return=[]){ array_walk_recursive($array,function($value)use(&$return){$return[]=$value;}); return $return; }
2022年06月17日
145 阅读
0 评论
0 点赞
1
2
3