It’s an inbuilt function of PHP. strpos() function Find the position of the first occurrence of a search_string in a string.
[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description
string, Required, String, Main string to check into.
search_string, Required, String, String to be searched for.
start, Optional, String, Specifies where to start searching in string.
[/table]
Returns the of the first occurrence of a search_string inside string. Also note that string positions start at 0, and not 1.
Returns FALSE if the search_string was not found.
[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
PHP Version, Description
7.1.0, Support for negative length has been added.
[/table]
<?php $mystring = 'tutor'; $findme = 't'; $pos = strpos($mystring, $findme); // Note our use of ===. Simply == would not work as expected // because the position of 't' 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"; } ?>
Output of above code in the browser is as below:
<?php // We can search for the character, ignoring anything before the offset $newstring = 'hello from tutor. hi all.'; $pos = strpos($newstring, 'h', 1); echo $pos; ?>
Output of above code in the browser is as below:
<?php // We can search for the character, ignoring anything before the offset $newstring = 'hello from tutor. hi all.'; $pos = strpos($newstring, 'b', 1); var_dump($pos); ?>
Output of above code is :
bool(false)
Toys have always played a crucial role in childhood, fueling imagination, creativity, and learning. Whether you're looking for educational playsets,…
Toys are an essential part of childhood, shaping creativity, learning, and fun-filled memories. Whether it’s the soft embrace of plush…
Toys are a gateway to imagination, creativity, and learning. Whether it’s a child’s first plush toy, a set of engaging…
Toys are more than just playthings; they help children explore, learn, and develop essential life skills. From interactive sensory toys…
Toys are more than just playthings; they are tools that foster creativity, motor skills, and cognitive development in children. From…
Toys play an integral role in a child’s growth and development. They spark creativity, encourage learning, and provide endless hours…