2011-02-02 14 views
7

Come posso ottenere tutte le lettere alfabetiche?Come posso ottenere tutte le lettere alfabetiche in PHP?

ho bisogno di ottenere tutte le lettere alfabetiche in un array come: array('a','b','c'...);

+2

In quale contesto? E quale alfabeto? –

+3

Perché non dovresti semplicemente scrivere l'array una volta e averlo finito? Potrebbe anche '$ arr = explode (" "," 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 ");' – Rudu

risposta

28

Utilizzare la funzione range come questo:

$letters = range('a', 'z'); 
print_r($letters); 

Risultato:

Array 
(
    [0] => a 
    [1] => b 
    [2] => c 
    [3] => d 
    [4] => e 
    [5] => f 
    [6] => g 
    [7] => h 
    [8] => i 
    [9] => j 
    [10] => k 
    [11] => l 
    [12] => m 
    [13] => n 
    [14] => o 
    [15] => p 
    [16] => q 
    [17] => r 
    [18] => s 
    [19] => t 
    [20] => u 
    [21] => v 
    [22] => w 
    [23] => x 
    [24] => y 
    [25] => z 
) 
+0

Grazie @Sarfraz – Chinmay235

+0

@Chinu: prego – Sarfraz

3
for($i=65; $i<=90; ++$i) print chr($i) 

65 è il codice ASCII per A

8

Utilizzare l'intervallo per fare ciò, il che lo rende facile.

$az = range('a', 'z'); 
3
$lower = 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'); 

$upper = 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'); 

Per i problemi che hanno soluzioni finite, a volte la cosa migliore che puoi fare è solo codificare ciascuno di essi.

1
$alphas = range('A', 'Z'); 
foreach($alphas as $value){ 
    echo $value."<br>"; 
} 

Stampa A-Z Risultato

provare questo ....

uscita è:

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
0

Si può anche fare questo con il mio esempio.

<?php 

$letters = '=======Alphabets========' . PHP_EOL; 
echo $letters; 

$alphabets = "A"; 

for ($i=0; $i < strlen($letters); $i++) { 
    echo "<br>".$alphabets++ . PHP_EOL; 
} 
Problemi correlati