How to count all the special characters on a string ? example:
$sample_string = "!!~~Sample string";
echo substr($sample_string, special character);
so the output will be 4.
How to count all the special characters on a string ? example:
$sample_string = "!!~~Sample string";
echo substr($sample_string, special character);
so the output will be 4.
$pattern = '/[!@#$%^&*()]/' // will match one occurrence of any symbol inside the []
int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
Perform a global regular expression match against a string. Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags.
After the first match is found, the subsequent searches are continued on from end of the last match.
You can simply use preg_replace_callback function along with closure like as
$sample_string = "!!~~Sample string";
$count = 0;
preg_replace_callback('/[^\h\w]/', function($m)use(&$count) {
$count++;
}, $sample_string);
echo $count;//4