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)
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…