Example 1 — Count Before a Character
Find how many characters come before the letter o.
<?php
$count = strcspn("hello world", "o");
echo $count, PHP_EOL; How It Works
- Pass a string and a stop list. The list is just
"o". - Walk from the start.
h,e,l,lare not in the list. - Stop at o. That is the fifth character.
- Return 4. Four characters came before it.

