Home >Backend Development >PHP Tutorial >PHP implements the method of finding substrings in a specified string, searching in a string_PHP tutorial
The example in this article describes the method of php implementing the method of finding substrings in a specified string. Share it with everyone for your reference. The specific analysis is as follows:
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.
<?php $haystack1 = "2349534134345w3mentor16504381640386488129"; $haystack2 = "w3mentor234953413434516504381640386488129"; $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"); ?>
Output result:
pos1 = (13); type is integer pos2 = (0); type is integer pos3 = (); type is boolean
pos3 returns a bool value, that is, the substring is not found
I hope this article will be helpful to everyone’s PHP programming design.