Way to get all alphabetic chars in an array in PHP?

后端 未结 14 769
情话喂你
情话喂你 2020-12-22 20:44

Is there a way to get all alphabetic chars (A-Z) in an array in PHP so I can loop through them and display them?

14条回答
  •  南方客
    南方客 (楼主)
    2020-12-22 21:08

    Maybe it's a little offtopic (topic starter asked solution for A-Z only), but for cyrrilic character soltion is:

    // to place letters into the array
    $alphas = array();
    foreach (range(chr(0xC0), chr(0xDF)) as $b) {
        $alphas[] = iconv('CP1251', 'UTF-8', $b);
    }    
    
    // or conver array into comma-separated string
    $alphas = array_reduce($alphas, function($p, $n) {
      return $p . '\'' . $n . '\',';
    });
    $alphas = rtrim($alphas, ',');
    
    // echo string for testing
    echo $alphas;
    // or echo mb_strtolower($alphas); for lowercase letters
    

提交回复
热议问题