miércoles, 6 de abril de 2016

php couting leters and numbers in an array

<?php



$str = '+0123qq';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);

echo "<br>";


$num=0;
$letters=0;

foreach($chars as $elements){

if (is_numeric($elements)) {
$num+=1;

}



else{
$letters+=1;

}



}

echo " array got $num  numbers";
echo "<br>";
echo " array got $letters  letters";

?>

preg_split (PHP 4, PHP 5, PHP 7) preg_split — Split string by a regular expression

Examples ¶

Example #1 preg_split() example : Get the parts of a search string
<?php// split the phrase by any number of commas or space characters,
// which include " ", \r, \t, \n and \f
$keywords preg_split("/[\s,]+/""hypertext language, programming");print_r($keywords);?>
The above example will output:
Array
(
    [0] => hypertext
    [1] => language
    [2] => programming
)
Example #2 Splitting a string into component characters
<?php
$str 
'string';$chars preg_split('//'$str, -1PREG_SPLIT_NO_EMPTY);print_r($chars);?>
The above example will output:
Array
(
    [0] => s
    [1] => t
    [2] => r
    [3] => i
    [4] => n
    [5] => g
)