#36: Finding Substrings
PHP has several functions that find a substring in a string. Your choice depends on what you want to do with the result. Here are three basic substring functions:
strpos()finds the position of the substring's first occurrence.strrpos()finds the position of the last occurrence of the substring. Use this function in conjunction withsubstr()to extract everything past that point in the string.strstr()returns everything after the first occurrence of the substring.
All three of these functions return False if the substring isn't present. Because both a position and a string can evaluate to False when used in a conditional, it's very important that you check the type of the return value, as shown in the following script:
<?php ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access