Find the first occurrence of a non-alphanumeric character in a string

I know what I could use strpos

to find the first occurrence of a string. But is it possible to find the first occurrence of a character that is not an alphabetic character or a number.

For example:

strpos2('hello world') => 5

strpos2('hi!you') => 2

+3


source to share


1 answer


Try with preg_match

$string = "hi!you";
preg_match('/[\W]+/', $string, $match, PREG_OFFSET_CAPTURE);
print_r($match);

      



Here $ match will return the position of the first matching alphabetic character

+3


source







All Articles