2012-09-04 21 views
6

In PHP esiste una funzione per suddividere la stringa in caratteri o array. ci O V E R F L O Wphp break string in caratteri

O

array( 
    0=> 'O', 
    1=> 'V', 
    2=> 'E', 
    3=> 'R', 
    4=> 'F', 
    5=> 'L', 
    6=> 'O', 
    7=> 'W' 
) 

o qualsiasi otherway è ..:

Example: OVERFLOW 

ho bisogno di spezzare il testo in eccesso sopra int?

+2

a punture di PHP può essere affrontata come un array –

+1

Posso chiedere perché? '$ str = 'overflow'; echo $ str [0]; 'echo' o 'così com'è. Puoi trattare qualsiasi stringa come un array senza prima dividerlo, ma in ogni caso –

+0

@Wooble Anche se questo può essere facilmente trovato su google, sarebbe comunque meglio se la quantità di domande su SO aumentasse, con domande più interessanti ogni giorno. – think123

risposta

6

C'è una funzione per questo: str_split

$broken = str_split("OVERFLOW", 1); 

Se la stringa può contenere caratteri multi-byte, utilizzare preg_split invece:

$broken = preg_split('##u', 'OVERFLOW', -1, PREG_SPLIT_NO_EMPTY); 
+0

sì il suo funzionamento .. Grazie per l'aiuto. – AnNaMaLaI

6

uso questa funzione --- str_split();

Questo dividerà la stringa in matrice di caratteri.

Esempio:

$word="overflow"; 
$split_word=str_split($word); 
+0

grazie ha aiutato ... – Aravin

3

Prova come questo ....

$var = "OVERFLOW"; 
echo $var[0]; // Will print "O". 
echo $var[1]; // Will print "V". 
2

Usa str_split

$str = "OVERFLOW" ; 
$var = str_split($str, 1); 
var_dump($var); 

uscita

array 
    0 => string 'O' (length=1) 
    1 => string 'V' (length=1) 
    2 => string 'E' (length=1) 
    3 => string 'R' (length=1) 
    4 => string 'F' (length=1) 
    5 => string 'L' (length=1) 
    6 => string 'O' (length=1) 
    7 => string 'W' (length=1) 

Esempio

1

Dirti la verità, è già rotto. Quindi questo dovrebbe funzionare:

$string = 'Hello I am the string.'; 
echo $string[0]; // 'H' 

Se specificamente desidera dividere esso, si potrebbe fare questo:

$string = 'Hello I am the string.'; 
$stringarr = str_split($string); 

Dipende se si ha realmente bisogno di dividere o no.

2

Si potrebbe fare:

$string = "your string"; 
$charArray = str_split($string); 
2

Date un'occhiata a str_split

Si può usare in questo modo:

array str_split (string $string [, int $split_length = 1 ]);