ananda ananda -4 years ago 49
PHP Question

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

I know that i could use

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 number.

For example:

strpos2('hello world') => 5

strpos2('hi!you') => 2

Answer Source

Try with preg_match

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download