PHP:Find Position
Jump to navigation
Jump to search
You can use the strpos function to find the position of a string within a string.
Find first occurrence
Use strpos($s,$t)
to find where $t occurs in $s.
$s strpos($s,"t") One two three 4 four five six
strpos($s,"t") gives the character position of the first "t" in $s.
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
O | n | e | t | w | o | t | h | r | e | e | ||
f | o | u | r | f | i | v | e | s | i | x |
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]
Find last occurrence
Use strrpos($s,$t)
to find the last place where $t occurs in $s.
$s strrpos($s,"t") One two three 8 four five six
strrpos($s,"t") gives the character position of the last "t".
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
O | n | e | t | w | o | t | h | r | e | e | ||
f | o | u | r | f | i | v | e | s | i | x |
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]
Find occurrence after ...
Use strpos($s,$t,$n)
to find where $t occurs in $s after $n.
$s index($s,"t",5) One two three 8 four five six
strpos($s,"t",5) gives the character position of the first "t" after position 5.
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
O | n | e | t | w | o | t | h | r | e | e | ||
f | o | u | r | f | i | v | e | s | i | x |
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]