PHP Function Strstr - Getting the First Occurrence of a Substring in a String
PHP strstr is a string function that finds the first occurrence of a substring inside another string. It returns the rest of the string (from the matching point), or FALSE, if the search string is not found in the given string. It is also a binary safe function, meaning it can be used on files without modifying them.
Often times, you may need to validate that an email address or a URL contains a specific word or letter. This could be as simple as ensuring that the string is properly formatted by searching for a specific character, or it could be something more complicated like checking whether a string contains a certain character pattern.
The php function strstr and its case-insensitive version, stristr, are the perfect tools to help you do this. They both find the first occurrence of a substring within a string, but they differ in how they do so. In this article, we will explore the use cases and syntax for
Input string: $string. Substring: $needle. Output string: if ($needle not in string): return FALSE. else: return str(string[1:]+"".join((string[:needle].replace("?",""))+string[needle+1:])