字符串

  1. 作者QQ:67065435 QQ群:821635552

  2. ip与数字进行转换

    <?php
    //数字转IP
    echo long2ip(16843009);
    //IP转数字
    echo ip2long("1.1.1.1");
    
  3. PHP获取字符串编码,并转为UTF-8

    <?php
    $str = '这是一个字符串';
    $str_encode = mb_detect_encoding($str);
    if($str_encode != 'UTF-8'){
        $str = mb_convert_encoding($str, 'UTF-8', $str_encode);
    }
    echo $str;
    
  4. UUID获取(先看php安装uuid扩展一节)

    <?php
    $uuid = uuid_create(1);
    
  5. php判断第四代身份证18位身份证号码最后一位是否正确

    <?php
    $id_num = '13333320001111222x';
    $sum = 0;
    $pow = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    for ($i = 0; $i < 17; $i++) {
       $num = (int)substr($id_num, $i, 1);
       $sum += $num * $pow[$i];
    }
    $end = (12 - ($sum % 11)) % 11;
    $end = $end < 10 ? (string)$end : 'x';
    echo $end;
    
  6. php判断客户端设备信息

    <?php
    $phone = '/android|webos|iphone|ipad|ipod|blackberry|symbianos|windows phone/';
    $public = '/micromessenger/';
    
    $use_type = strtolower($_SERVER['HTTP_USER_AGENT']);
    if (preg_match($phone, $use_type)) {
    echo '是手机';
    }elseif(preg_match($public, $use_type)){
    echo '公众号';
    } else {
    echo '是电脑';
    }
    
  7. php获取ip地址不能获取返回0.0.0.0

    <?php
    function getIP()
    {
    if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), '0.0.0.0')) {
    $ip_addr = getenv('HTTP_CLIENT_IP');
    } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), '0.0.0.0')) {
    $ip_addr = getenv('HTTP_X_FORWARDED_FOR');
    } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), '0.0.0.0')) {
    $ip_addr = getenv('REMOTE_ADDR');
    } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], '0.0.0.0')) {
    $ip_addr = $_SERVER['REMOTE_ADDR'];
    }else{
    $ip_addr = '0.0.0.0';
    }
    preg_match("/[\d\.]{7,15}/", $ip_addr, $ip_matches);
    $ip_addr = empty($ip_matches[0]) ? '0.0.0.0' : $ip_matches[0];
    return $ip_addr;
    }
    
  8. php判断是否为url

    <?php
    /**
     * 英文域名url正则
     *
     * 传输协议(字母开头紧接'://')
     * ^[a-z]+:\/\/
     *
     * 多级子域(字母、数字开头可以包含字母、数字、'_'、'-'且以'.'结尾的多组这种组合)
     * ([a-z0-9]{1}[a-z0-9_-]*\.)*
     *
     * 顶级域名(由.分割的1到多组字母、数字组合)
     * ([a-z0-9]+\.)*[a-z0-9]+
     *
     * 链接路由(以'/'开头的一段字符串)
     * (\/[^\f\n\r\t\v]*)*$
     *
     * 最终表达式
     * ^[a-z]+:\/\/([a-z0-9]{1}[a-z0-9_-]*\.)*([a-z0-9]+\.)*[a-z0-9]+(\/[^\f\n\r\t\v]*)*$
     */
    $url = "https://www.baidu.com";
    $regular = '/^[a-z]+:\/\/([a-z0-9]{1}[a-z0-9_-]*\.)*([a-z0-9]+\.)*[a-z0-9]+(\/[^\f\n\r\t\v]*)*$/';
    preg_match($regular, $url, $match);
    if (isset($match[0]) && $url == $match[0]) {
        return true;
    } else {
        return false;
    }
    
  9. php字符串类型判断

    <?php
    # 判断一个变量是否为整数、小数、布尔值
    $str = '123456789';
    $is_rule = filter_var($str, FILTER_VALIDATE_INT);
    $is_rule = filter_var($str, FILTER_VALIDATE_FLOAT);
    $is_rule = filter_var($str, FILTER_VALIDATE_BOOLEAN);
    
    # 判断一个变量是否为IP地址、MAC地址、URL、邮箱
    $str = '123456789@qq.com';
    $is_ip = filter_var($str, FILTER_VALIDATE_IP);
    $is_mac = filter_var($str, FILTER_VALIDATE_MAC);
    $is_url = filter_var($str, FILTER_VALIDATE_URL);
    $is_email = filter_var($str, FILTER_VALIDATE_EMAIL);
    
    # 判断一个邮箱域名是否可用
    $domain = 'qq.com';
    $have_mx = checkdnsrr($domain, 'MX');
    
  10. php敏感词屏蔽

    <?php
    $str = 'ajbkcldmenf';
    //敏感词先分3类
    $bds1 = '/a|b|j/';
    $bds2 = '/c|d|k/';
    $bds3 = '/e|f|l/';
    $str = preg_replace($bds1,'*',$str);
    $str = preg_replace($bds2,'#',$str);
    $str = preg_replace($bds3,'?',$str);
    echo $str;
    
  11. php输出随机中文

    <?php
    $length = 3;
    $unicode = "";
    for ($i = 1; $i <= $length; $i++) {
        $number = rand(0x4e00, 0x9fa5);
        $number = dechex($number);
        $unicode .= "\\u{$number}";
    }
    $chinese = json_decode("[\"{$unicode}\"]", true);
    $chinese = $chinese[0];
    echo $chinese;
    
  12. php输出自定义名称

    <?php
    //随机获取姓氏
    $xing = ["赵","钱","孙","李","周","吴","郑","王","冯","陈","褚","卫","蒋","沈","韩","杨","朱","秦","尤","许","何","吕","施","张","孔","曹","严","华","金","魏","陶","姜","戚","谢","邹","喻","柏","水","窦","章","云","苏","潘","葛","奚","范","彭","郎","鲁","韦","昌","马","苗","凤","花","方","俞","任","袁","柳","酆","鲍","史","唐","费","廉","岑","薛","雷","贺","倪","汤","滕","殷","罗","毕","郝","邬","安","常","乐","于","时","傅","皮","卞","齐","康","伍","余","元","卜","顾","孟","平","黄","和","穆","萧","尹","姚","邵","湛","汪","祁","毛","禹","狄","米","贝","明","臧","计","伏","成","戴","谈","宋","茅","庞","熊","纪","舒","屈","项","祝","董","梁","杜","阮","蓝","闵","席","季","麻","强","经","房","裘","缪","干","解","应","宗","丁","宣","贲","邓","郁","单","杭","洪","包","诸","左","石","崔","吉","钮","龚","程","嵇","邢","滑","裴","陆","荣","翁","荀","羊","於","惠","甄","曲","家","封","芮","羿","储","靳","汲","邴","糜","松","井","段","富","巫","乌","焦","巴","弓","牧","隗","山","谷","车","侯","宓","蓬","全","郗","班","仰","秋","仲","伊","宫","宁","仇","栾","暴","甘","钭","厉","戎","祖","武","符","刘","景","詹","束","龙","索","咸","籍","赖","卓","蔺","屠","蒙","池","乔","阴","郁","胥","能","苍","双","闻","莘","党","翟","谭","贡","劳","逄","姬","申","扶","堵","冉","宰","郦","雍","却","璩","桑","桂","濮","牛","寿","通","边","扈","燕","冀","郏","浦","尚","农","温","别","庄","晏","柴","瞿","阎","充","慕","连","茹","习","宦","艾","鱼","容","向","古","易","慎","戈","廖","庾","终","暨","居","衡","步","都","耿","满","弘","匡","国","文","寇","广","禄","阙","东","欧","殳","沃","利","蔚","越","夔","隆","师","巩","厍","聂","晁","勾","敖","融","冷","訾","辛","阚","那","简","饶","空","曾","毋","沙","乜","养","鞠","须","丰","巢","关","蒯","相","查","后","荆","红","游","竺","权","逯","盖","益","桓","公","闻人","东方","赫连","皇甫","尉迟","公羊","东郭","南门","呼延","归","海","羊舌","微生","岳","帅","缑","亢","况","郈","有","琴","梁丘","左丘","东门","西门","商","牟","佘","佴","伯","赏","南宫","墨","哈","谯","笪","年","爱","阳","佟","第五","言","福"];
    $xing = $xing[rand(0, count($xing) - 1)];
    
    //随机获取字辈
    $long = 3 - mb_strlen(rand(0, 10));
    $bei = "伯伟尚嘉芳浩天大发祥闻知尊典训正直集材梁礼义为贤度谦恭有道光齐家先孝友之国尚文章学问通经济功名奏天堂祖训欣永振百世受其昌";
    $bei = $long == 2 ? mb_substr($bei, rand(0, mb_strlen($bei) - 1), 1) : "";
    
    //随机获取名字
    $ming = "涛昌进林有坚和彪博诚先敬震振壮会群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发成康星光天达安岩中茂武新利清飞彬富顺信子杰楠榕风航弘男女嘉琼桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊勤珍贞莉兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳";
    $ming = mb_substr($ming, rand(0, mb_strlen($ming) - 1), 1);
    
    $name = "{$xing}{$bei}{$ming}";
    echo $name;
    
  13. php数字转中文

    <?php
    function small_number_to_chinese($num = 0)
    {
        if (empty($num)) {
        return "零";
       }
        if ($num > 9999) {
           return "数字过大,无法解析。";
        }
        $encode = 'UTF-8';
        $arr1 = array(
            '0' => '零',
            '1' => '一',
            '2' => '二',
            '3' => '三',
            '4' => '四',
            '5' => '五',
            '6' => '六',
            '7' => '七',
            '8' => '八',
            '9' => '九'
        );
        $arr2 = array(
            '1' => "",
            '2' => "十",
            '3' => "百",
            '4' => "千"
        );
        $len = strlen($num);
        $res = '';
        for ($i = 1; $i <= $len; $i++) {
            $str = substr($num, -$i, 1);
            $zh = $arr1[$str];
            $res = $zh . $arr2[$i] . $res;
        }
        $res = str_replace("零千", "零", $res);
        $res = str_replace("零百", "零", $res);
        $res = str_replace("零十", "零", $res);
        $res = str_replace("零零", "零", $res);
        $res = str_replace("零零", "零", $res);
        if (mb_substr($res, -1, 1, $encode) == "零") {
           $res = mb_substr($res, 0, -1, $encode);
        }
        return $res;
    }
    function number_to_chinese($num = 0)
    {
        if (empty($num)) {
           return "零";
        }
        $len = strlen($num);
        if ($len > 12) {
           return "该数字过大,无法解析。";
        }
        $arr = array(
            '4' => '',
            '8' => '万',
            '12' => '亿'
        );
        $res = '';
        for ($i = 4; $i <= $len; $i += 4) {
            $str = substr($num, -$i, 4);
            $str = (int)$str;
            $zh = small_number_to_chinese($str);
            $add = '';
            $hex = $arr[$i];
            if($str < 1000 && $str != 0){
               $add = "零";
            }
            if($str == 0){
                $zh = '';
                $hex = '';
            }
            $res = $add . $zh . $hex . $res;
        }
        if ($i - 4 < $len) {
            $len = $len - ($i - 4);
            $str = substr($num, 0, $len);
            $zh = small_number_to_chinese($str);
            $res = $zh . $arr[$i] . $res;
        }
        return $res;
    }
    //测试
    $a = 9;
    if($a <= 9999){
        echo small_number_to_chinese($a);
    }else{
        echo number_to_chinese($a);
    }
    
  14. php中文转数字

    <?php
    function strKToNum($str)
    {
        //只能处理千以内整数
        if (strstr($str, "万")) {
            return 0;
        }
        if($str == '零'){
            return 0;
        }
        $str = str_replace("一", "1", $str);
        $str = str_replace("二", "2", $str);
        $str = str_replace("三", "3", $str);
        $str = str_replace("四", "4", $str);
        $str = str_replace("五", "5", $str);
        $str = str_replace("六", "6", $str);
        $str = str_replace("七", "7", $str);
        $str = str_replace("八", "8", $str);
        $str = str_replace("九", "9", $str);
    
        $str = preg_replace("/千$/", "000", $str);
    
        $str = preg_replace("/千零1十$/", "010", $str);
        $str = preg_replace("/千零2十$/", "020", $str);
        $str = preg_replace("/千零3十$/", "030", $str);
        $str = preg_replace("/千零4十$/", "040", $str);
        $str = preg_replace("/千零5十$/", "050", $str);
        $str = preg_replace("/千零6十$/", "060", $str);
        $str = preg_replace("/千零7十$/", "070", $str);
        $str = preg_replace("/千零8十$/", "080", $str);
        $str = preg_replace("/千零9十$/", "090", $str);
    
        $str = str_replace("千零1十", "01", $str);
        $str = str_replace("千零2十", "02", $str);
        $str = str_replace("千零3十", "03", $str);
        $str = str_replace("千零4十", "04", $str);
        $str = str_replace("千零5十", "05", $str);
        $str = str_replace("千零6十", "06", $str);
        $str = str_replace("千零7十", "07", $str);
        $str = str_replace("千零8十", "08", $str);
        $str = str_replace("千零9十", "09", $str);
    
        $str = str_replace("千零1", "001", $str);
        $str = str_replace("千零2", "002", $str);
        $str = str_replace("千零3", "003", $str);
        $str = str_replace("千零4", "004", $str);
        $str = str_replace("千零5", "005", $str);
        $str = str_replace("千零6", "006", $str);
        $str = str_replace("千零7", "007", $str);
        $str = str_replace("千零8", "008", $str);
        $str = str_replace("千零9", "009", $str);
    
        $str = str_replace("千", "", $str);
    
        $str = preg_replace("/百$/", "00", $str);
    
        $str = str_replace("百零", "0", $str);
    
        $str = str_replace("百", "", $str);
    
        $str = preg_replace("/1十$/", "10", $str);
        $str = preg_replace("/2十$/", "20", $str);
        $str = preg_replace("/3十$/", "30", $str);
        $str = preg_replace("/4十$/", "40", $str);
        $str = preg_replace("/5十$/", "50", $str);
        $str = preg_replace("/6十$/", "60", $str);
        $str = preg_replace("/7十$/", "70", $str);
        $str = preg_replace("/8十$/", "80", $str);
        $str = preg_replace("/9十$/", "90", $str);
    
        $str = str_replace("1十", "1", $str);
        $str = str_replace("2十", "2", $str);
        $str = str_replace("3十", "3", $str);
        $str = str_replace("4十", "4", $str);
        $str = str_replace("5十", "5", $str);
        $str = str_replace("6十", "6", $str);
        $str = str_replace("7十", "7", $str);
        $str = str_replace("8十", "8", $str);
        $str = str_replace("9十", "9", $str);
    
        $str = preg_replace("/十$/", "10", $str);
    
        $str = str_replace("十", "1", $str);
    
        $str = str_replace("零", "", $str);
        return $str;
    }
    function strAToNum($str)
    {
        //只能处理千亿以内整数
        if (strstr($str, "万亿")) {
            return 0;
        }
        $arr = explode("万", $str);
        if (count($arr) == 1) {
            $num = strKToNum($arr[0]);
            return $num;
        } else {
            $num = strKToNum($arr[1]);
            $arr = explode("亿", $arr[0]);
            if (count($arr) == 1) {
                $num = strKToNum($arr[0]) . substr("000" . $num, -4);
                return $num;
            } else {
                $num = strKToNum($arr[0]) . substr("000" . strKToNum($arr[1]), -4) . substr("000" . $num, -4);
                return $num;
            }
        }
    }
    echo strAToNum('零') . PHP_EOL;
    echo strAToNum('十') . PHP_EOL;
    echo strAToNum('十二') . PHP_EOL;
    echo strAToNum('一十二') . PHP_EOL;
    echo strAToNum('八') . PHP_EOL;
    echo strAToNum('八十') . PHP_EOL;
    echo strAToNum('八十八') . PHP_EOL;
    echo strAToNum('八百') . PHP_EOL;
    echo strAToNum('八百零八') . PHP_EOL;
    echo strAToNum('八百八十') . PHP_EOL;
    echo strAToNum('八百八十八') . PHP_EOL;
    echo strAToNum('八千零八') . PHP_EOL;
    echo strAToNum('八千零八十') . PHP_EOL;
    echo strAToNum('八千零八十八') . PHP_EOL;
    echo strAToNum('八千八百') . PHP_EOL;
    echo strAToNum('八千八百零八') . PHP_EOL;
    echo strAToNum('八千八百八十') . PHP_EOL;
    echo strAToNum('八千八百八十八') . PHP_EOL;
    echo strAToNum('八千亿八千八百万零八十八') . PHP_EOL;
    
  15. php章节名去除第N章

    <?php
    $chapter_name = "第123章 我勒个去";
    //$regular1 = "/[^一-龥a-zA-Z0-9()()]+/";
    $regular1 = "/[^\x{4e00}-\x{9fa5}a-zA-Z0-9()()]+/u";
    $regular2 = "/^[零〇两俩一二三四五六七八九十百千壹贰叁肆伍陆柒捌玖拾佰0-9]+(章|节|话|回|卷)/";
    $regular3 = "/^第(.*?)(章|节|话|回|卷)/";
    $chapter_name = preg_replace($regular1, '', $chapter_name);
    $chapter_name = preg_replace($regular2, '', $chapter_name);
    $chapter_name = preg_replace($regular3, '', $chapter_name);
    $chapter_name = !empty($chapter_name) ? $chapter_name : " ";
    echo $chapter_name;
    
  16. php章节内容整理格式

    <?php
    $chapter_content = "段落1内容\r\n段落2内容\r\n段落3内容\r\n";
    $chapter_content = str_replace('</p>', "\n", $chapter_content);
    $chapter_content = str_replace('<br>', "\n", $chapter_content);
    $chapter_content = str_replace('<br/>', "\n", $chapter_content);
    $chapter_content = strip_tags($chapter_content);
    $chapter_content = trim((string)$chapter_content);
    $chapter_content = str_replace("—", '', $chapter_content);
    $chapter_content = str_replace(" ", '', $chapter_content);
    $chapter_content = preg_replace('/[^\S\r\n]+/', '', $chapter_content);
    $chapter_content = preg_replace('/[\r\n]+/', "\r\n", $chapter_content);
    $chapter_content = trim((string)$chapter_content);
    echo $chapter_content;
    
  17. php书籍文本按章处理为数组

    <?php
    $start = 0;
    $regular = '/第\d+章(.*?)\r\n|第[零一二三四五六七八九十百千万亿]+章(.*?)\r\n/';
    //test.txt为小说文本
    $str = file_get_contents(__DIR__ . '/test.txt');
    $str = str_replace('—','--',$str);
    $str = preg_replace('/[^\r\n\S]+/', "", $str);
    $str = preg_replace('/[\r\n]+/', "\r\n", $str);
    //获取章节名列表
    preg_match_all($regular, $str, $chanter_names, PREG_PATTERN_ORDER, 0);
    $chanter_names = $chanter_names[0];
    //获取章节内容列表
    $chanter_contents = preg_split($regular, $str, null);
    array_shift($chanter_contents);
    //判断处理后的格式是否合适
    //处理后的格式不合适则不处理
    $total_names = count($chanter_names);
    $total_contents = count($chanter_contents);
    if ($total_names != $total_contents) {
       die("章节文件格式有误{$total_names}_{$total_contents}");
    }
    //处理后的格式合适则处理
    $chapters = [];
    foreach ($chanter_names as $key => &$chanter_name) {
        $chapter_order = $key + $start + 1;
        $chanter_name = str_replace("\r\n", "", $chanter_name);
        $chanter_name = preg_replace('/第\d+章|第[零一二三四五六七八九十百千万亿]+章/', "第{$chapter_order}章 ", $chanter_name);
        $chapters[] = [
            'chapter_order' => $key + 1,
            'chapter_name' => $chanter_name,
            'chapter_content' => mb_substr($chanter_contents[$key], 0, 10)
        ];
    }
    print_r($chapters);
    
  18. php章节内容转html可显示的unicode(10进制码)

    <?php
    function unicode_encode($from_str = "", $zone = "")
    {
        if (empty($from_str)) {
            return "";
        }
        if ($zone == 'zh_cn') {
            //$pattern = '/[一-龥]{1}/u';
            $pattern = '/[\x{4e00}-\x{9fa5}]{1}/u';
        } elseif ($zone == 'en_uf') {
            $pattern = '/[A-Za-z]{1}/u';
        } else {
            $pattern = '/./u';
        }
        preg_match_all($pattern, $from_str, $search);
        $replace = [];
        $search = $search[0];
        $search = array_unique($search);
        foreach ($search as $s) {
            $replace[] = "&#" . hexdec(bin2hex(mb_convert_encoding($s, "UCS-4"))) . ";";
        }
        $res_str = str_replace($search, $replace, $from_str);
        unset($search, $replace);
        return $res_str;
    }
    //全部转换
    echo unicode_encode("大家好,我叫PHP");
    //只转中文
    echo unicode_encode("大家好,我叫PHP", 'zh_cn');
    //只转英文
    echo unicode_encode("大家好,我叫PHP", 'en_uf');
    
Copyright © 豆包嘿嘿~ 2012-∞ 冀ICP备17033181号 all right reserved,powered by Gitbook修订: 2020-06-17 13:39:00

results matching ""

    No results matching ""