字母作为字符串...如何?

时间:2019-06-01 03:04:14

标签: php alphabet

我一直在寻找和搜索很长一段时间,但是我在PHP中找不到任何方法来获取诸如“ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ”之类的字符串。 (不确定哪个是相关的...也许两者都有)。实际上,我什至在任何地方都找不到这些PHP的非PHP list 。除了美国。

我是否必须自己弄清楚然后生成自己的字符串,还是将其内置到语言中并准备好“抓取”?

1 个答案:

答案 0 :(得分:0)

如果您知道字母表的第一个字母和最后一个字母,可以使用mb_strtolower和我在这里找到的mb_range函数的组合:GitHub rodneyrehm

$low_letters = mb_range('α', 'ω');
$upper_letters = array_map('mb_strtoupper', $low_letters);

echo implode('', array_merge($low_letters, $upper_letters)); // αβγδεζηθικλμνξοπρςστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΣΤΥΦΧΨΩ

还有mb_range函数的代码(以防GitHub消失):

 mb_internal_encoding('UTF-8');
/**
 * multibyte string compatible range('A', 'Z')
 *
 * @param string $start Character to start from (included)
 * @param string $end Character to end with (included)
 * @return array list of characters in unicode alphabet from $start to $end
 * @author Rodney Rehm
 */
function mb_range($start, $end) {
    // if start and end are the same, well, there's nothing to do
    if ($start == $end) {
        return array($start);
    }

    $_result = array();
    // get unicodes of start and end
    list(, $_start, $_end) = unpack("N*", mb_convert_encoding($start . $end, "UTF-32BE", "UTF-8"));
    // determine movement direction
    $_offset = $_start < $_end ? 1 : -1;
    $_current = $_start;
    while ($_current != $_end) {
        $_result[] = mb_convert_encoding(pack("N*", $_current), "UTF-8", "UTF-32BE");
        $_current += $_offset;
    }
    $_result[] = $end;
    return $_result;
}