You can use something like the following to count the number of times a character from a given set of characters appears within a string:
<?php
/**
* Count the number of times any char from $char is found in $search
* @param $search Looks for the chars here
* @param $chars The chars to look for
* @return int
*/
function countChars($search, $chars)
{
$chars = str_replace(
array("\\", '[', ']', '^', '-'),
array("\\\\", '\[', '\]', '\^', '\-'), $chars);
$results = array();
preg_match_all("/[$chars]/", $search, $results, PREG_SET_ORDER);
return count($results);
}
var_dump(countChars("Hello, World", "ol"));
var_dump(countChars("Lorem ipsum...", ".m")); // searches for . and m only
var_dump(countChars("^[]-^\\*", "^[]-\\"));
Hope that helps.