The built in function “strpos” will find the first occurrence in a string. This function accepts 3 parameters: 1. The subject (string), 2. The needle (what are you looking for ?), and the last one will be the offset (define a string position you want to search from).
You need to know that the first string position (first character in string) begins at 0.
As you can see at the examples below, the use of just one single character is not recommended as it may occur earlier then you expect. If you know what you are looking for, use patterns.
<?php // The test phrase $string = 'This is just a simple phrase!'; // Locate the string position of "simple" $simple = strpos($string, 'sim'); // Echo 15 echo $simple; echo '<br />'; // locate the first s $s = strpos($string, 's'); // Echo 3 echo $s; echo '<br />'; // Locate the first "s" from string position 16 $s_from_16 = strpos($string, 's', 16); // Echo 26 echo $s_from_16; ?>
This will result in….
15
3
26
3
26
Mail this!
- Comments (0)
- PingBacks (0)
- TrackBacks (0)


» Latest comments