How to count the total number of punctuation characters in a string with PHP

1 Answer

0 votes
$str = "PHP is a general-purpose scripting language for web development.";  

$count = 0;  
for ($i = 0; $i < strlen($str); $i++) {  
    if ($str[$i] == '.' || $str[$i] == ',' || $str[$i] == ';' || 
        $str[$i] == '?'  || $str[$i] == '!' || $str[$i] == ':' || 
        $str[$i] == '\'' || $str[$i] ==   '\"' || $str[$i] == '-') {  
        $count++;  
    }  
}  

echo $count; 




/*
run:
 
2
 
*/

 



answered Sep 27, 2021 by avibootz
...