The strpos() function can be used to find substrings in php. The strpos() function will try to find the first occurrence of a substring in the source string. If found, it returns a non-negative integer indicating the position of the substring. Otherwise it returns a boolean false.
- $haystack1 = "2349534134345w3mentor16504381640386488129";
- $haystack2 = "w3mentor2349534134345165043816403864881 29";
- $haystack3 = "center234953413434516504381640386488129fyi";
- $pos1 = strpos($haystack1, "w3mentor");
- $pos2 = strpos($haystack2, "w3mentor");
- $pos3 = strpos($haystack3, "w3mentor");
- print("pos1 = ($pos1); type is " . gettype($pos1) . "n") ;
- print("pos2 = ($pos2); type is " . gettype($pos2) . "n");
- print("pos3 = ($pos3); type is " . gettype($pos3) . "n ");
- ?>
Copy code
Output result
- pos1 = (13); type is integer
- pos2 = (0); type is integer
- pos3 = (); type is boolean
-
- pos3 returns a bool value, that is, no substring was found
Copy code
|