![]() ![]() Note that both of the functions we just went over are case-sensitive. Once again, if strstr() returns anything other than false, it means that the substring is present in the string. In the above code, we’re checking if the string $string contains the substring $substring. $string = "Hello, world!" $substring = "world" if ( strstr ($string, $substring)) If the substring is not found, it returns false. The strpos() function, short for “string position,” returns the position of the first occurrence of a substring within a string. In PHP, the two best ways to check if a string contains a substring are by using the built-in strpos() and strstr() functions. By the time you’re done reading, you’ll have good knowledge of how to check strings for substrings in PHP, and you’ll be ready to use this skill in your own code. We’ll keep things simple and easy to understand, so even if you’re new to PHP, you’ll be able to follow along. Don’t worry if you’re not familiar with these functions yet - I’ll explain how to use them step by step and which one to go for. In this tutorial, I’ll show you how to do this using two functions: strpos() and strstr(). Whatever the reason, it’s a common task in PHP programming. ![]() Have you ever needed to check if a certain word or phrase is present in a larger string in your PHP code? Maybe you need to filter user input or perform a search on a website.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |