2

I know that 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 number.

For example:

strpos2('hello world') => 5

strpos2('hi!you') => 2

Moradnejad
  • 3,466
  • 2
  • 30
  • 52

1 Answers1

3

Try with preg_match

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

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

B. Desai
  • 16,414
  • 5
  • 26
  • 47