-
Notifications
You must be signed in to change notification settings - Fork 0
/
Digits.php
45 lines (34 loc) · 1.18 KB
/
Digits.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
class Digits{
public static function countAlls(string $_words)
{
//pour supprimer seulement les espaces on utilise str_replace(' ','', $myWords);
//pour supprimer les tabulations les sursaut de lignes etc, on utilise:
//$str = "This is a simple \npiece\tof text.";
//$new_str = preg_replace("/\s+/", "", $str);
// var_dump($_words);
$words = preg_replace("/\s+/", "", $_words);
$arrWords = str_split($words);
$letter = 0;
$number = 0;
// var_dump($words);
foreach($arrWords as $arrWord){
//filter_var : Filtre une variable avec un filtre spécifique
//FILTER_VALIDATE_INT : Vérifiez si la variable $ int est un entier:
if (filter_var($arrWord, FILTER_VALIDATE_INT)){
$number ++;
}else{
$letter ++;
}
}
return ['number' => $number, 'letter' => $letter];
}
// echo $letter;
// echo $number;
public static function checkIsNumber($_value)
{
return filter_var($_value, FILTER_VALIDATE_INT) !== false;
}
}
$mots ='Hell8877 Worl';
print_r( Digits::countAlls($mots) );