当前位置: 首页> 八字命理> 正文

PHP实现身份证号解析:验证、查询星座、生肖及性别功能

PHP实现身份证号解析:验证、查询星座、生肖及性别功能

<?php // 嘿,想不想知道身份证里藏着啥秘密?来,这个PHP函数帮你一把! function get_xingzuo($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 <= 21) || ($month == 2 && $day <= 19)) { $strValue = "水瓶座"; } elseif (($month == 2 && $day > 20) || ($month == 3 && $day <= 20)) { $strValue = "双鱼座"; } elseif (($month == 3 && $day > 20) || ($month == 4 && $day <= 20)) { $strValue = "白羊座"; } elseif (($month == 4 && $day > 20) || ($month == 5 && $day <= 21)) { $strValue = "金牛座"; } elseif (($month == 5 && $day > 21) || ($month == 6 && $day <= 21)) { $strValue = "双子座"; } elseif (($month == 6 && $day > 21) || ($month == 7 && $day <= 22)) { $strValue = "巨蟹座"; } elseif (($month == 7 && $day > 22) || ($month == 8 && $day <= 23)) { $strValue = "狮子座"; } elseif (($month == 8 && $day > 23) || ($month == 9 && $day <= 23)) { $strValue = "处女座"; } elseif (($month == 9 && $day > 23) || ($month == 10 && $day <= 23)) { $strValue = "天秤座"; } elseif (($month == 10 && $day > 23) || ($month == 11 && $day <= 22)) { $strValue = "天蝎座"; } elseif (($month == 11 && $day > 22) || ($month == 12 && $day <= 21)) { $strValue = "射手座"; } elseif (($month == 12 && $day > 21) || ($month == 1 && $day <= 20)) { $strValue = "摩羯座"; } return $strValue; } // 哎,你知道自己的生肖吗?这个函数帮你搞定! function get_shengxiao($cid) { if (!isIdCard($cid)) return ''; $start = 1901; $end = (int)substr($cid, 6, 4); $x = ($start - $end) % 12; $value = ""; if ($x == 1 || $x == -11) { $value = "鼠"; } elseif ($x == 0) { $value = "牛"; } elseif ($x == 11 || $x == -1) { $value = "虎"; } elseif ($x == 10 || $x == -2) { $value = "兔"; } elseif ($x == 9 || $x == -3) { $value = "龙"; } elseif ($x == 8 || $x == -4) { $value = "蛇"; } elseif ($x == 7 || $x == -5) { $value = "马"; } elseif ($x == 6 || $x == -6) { $value = "羊"; } elseif ($x == 5 || $x == -7) { $value = "猴"; } elseif ($x == 4 || $x == -8) { $value = "鸡"; } elseif ($x == 3 || $x == -9) { $value = "狗"; } elseif ($x == 2 || $x == -10) { $value = "猪"; } return $value; } // 嘿,身份证还能看出性别呢! function get_xingbie($cid) { if (!isIdCard($cid)) return ''; $sexint = (int)substr($cid, 16, 1); return $sexint % 2 === 0 ? '女' : '男'; } // 哎呀,身份证号对不对啊?这个函数帮你检查一下! function isIdCard($number) { $number = strtoupper($number); $wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); $ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); $sigma = 0; for ($i = 0; $i < 17; $i++) { $b = (int)$number{$i}; $w = $wi[$i]; $sigma += $b * $w; } $snumber = $sigma % 11; $check_number = $ai[$snumber]; return $number{17} == $check_number; } ?>

  思考一下: 你知道身份证号的第17位数字代表什么吗?欢迎在评论区分享你的发现!

阅读全文