在PHP中创建随机UTF

在PHP中创建随机UTF

本文介绍了在PHP中创建随机UTF-8字符串的最佳功能? (仅字母字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我写了这个函数,它创建了一个UTF-8字符随机字符串.它运作良好,但正则表达式[^\p{L}]似乎并未过滤所有非字母字符.我想不出一种更好的方法来生成不带非字母字符的完整unicode范围.缺少手动搜索并定义65到65533之间的十进制字母的范围.

I wrote this function that creates a random string of UTF-8 characters. It works well, but the regular expression [^\p{L}] is not filtering all non-letter characters it seems. I can't think of a better way to generate the full range of unicode without non-letter characters.. short of manually searching for and defining the decimal letter ranges between 65 and 65533.

function rand_str($max_length, $min_length = 1, $utf8 = true) {
    static $utf8_chars = array();

    if ($utf8 && !$utf8_chars) {
        for ($i = 1; $i <= 65533; $i++) {
            $utf8_chars[] = mb_convert_encoding("&#$i;", 'UTF-8', 'HTML-ENTITIES');
        }

        $utf8_chars = preg_replace('/[^\p{L}]/u', '', $utf8_chars);

        foreach ($utf8_chars as $i => $char) {
            if (trim($utf8_chars[$i])) {
                $chars[] = $char;
            }
        }

        $utf8_chars = $chars;
    }

    $chars = $utf8 ? $utf8_chars : str_split('abcdefghijklmnopqrstuvwxyz');
    $num_chars = count($chars);
    $string = '';

    $length = mt_rand($min_length, $max_length);

    for ($i = 0; $i < $length; $i++) {
        $string .= $chars[mt_rand(1, $num_chars) - 1];
    }

    return $string;
}

推荐答案

\p{L}可能捕获了太多内容.尝试限制为{Ll}和{LU}-{L}包括{Lo}-其他.

\p{L} might be catching too much. Try to limit to {Ll} and {LU} -- {L} includes {Lo} -- others.

这篇关于在PHP中创建随机UTF-8字符串的最佳功能? (仅字母字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:13