The position of "my"

$myVar="This is [COLOR="#FF0000"]my[/COLOR] book"

I have a variable named “myVar” like the above.

The position of “This” is 1 because the first letter “T” of the word “This” comes 1st in the variable.
The position of “is” is 6 because the first letter “i” of the word “is” comes 6th in the variable.
The position of “my” is 9 because the first letter “m” of the word “my” comes 9th in the variable.
The position of “book” is 12 because the first letter “b” of the word “book” comes 12th in the variable.

The following would-be code doesn’t work correctly, but I hope it shows what I want.

[B]code[/B]
$myVar="This is [COLOR="#FF0000"]my[/COLOR] book"

$find_the_postion_of_my=find_the_postion_of_my($myVar,"[COLOR="#FF0000"]my[/COLOR]");

echo $find_the_postion_of_[COLOR="#FF0000"]my[/COLOR];

[B]result[/B]
9

How can I get the position “9”?

Take a look at PHP Manual: strpos()

// “http://php.net/manual/en/function.strpos.php


$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}

[ot]

Just remember that numbering starts from zero, so the first letter’s position is 0. [/ot]