In PHP, to search for a specific text within a string a function strpos() is used. It includes 3 parameters:
- string: It is required parameter which specify the string to search.
- find: It is also a required parameter which specify the string to find.
- start: It is an optional field that specify where to begin the search.
Syntax:
strpos(string, find, start)
It returns the position of the first occurence of the string inside another or false if the string not found. The position of the string start at 0.
For example:
<?php
$str = "String in which you search the sub-string" ;
$find="sub";
echo strpos($str, $find);
?>
The output of the above code is:
31
0 Comment(s)