Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

People also ask

How can I get only characters in a string in PHP?

$result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);

How do I get the first character of a string in PHP?

To get the first character from a string, we can use the substr() function by passing 0,1 as second and third arguments in PHP.


$alphas = range('A', 'Z');

Documentation: https://www.php.net/manual/en/function.range.php


To get both upper and lower case merge the two ranges:

$alphas = array_merge(range('A', 'Z'), range('a', 'z'));

$alphabet = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

Another way:

$c = 'A';
$chars = array($c);
while ($c < 'Z') $chars[] = ++$c;