I need to find out how many starting consonants a word has. The number is used later in the program. The code below does work, I am wondering if it is possible to do this with a regular expression.
$var = strspn($mystring, $mystring2);
Using a regular expression as this
$var = strspn($mystring, $regex);
Does not work as strspn expects two strings.
Best How To :
This is one way to do it, using
preg_match('/^[b-df-hj-np-tv-z]*/i', $string, $matches);
$count = strlen($matches);
The regular expression matches zero or more (
*) case-insensitive (
[b-df-hj-np-tv-z] at the beginning (
^) of the string and stores the matched content in the
$matches array. Then it's just a matter of getting the length of the matched string using
strlen. Not the most elegant solution, but I believe it answers your question.