PHP to check substring in a string Last Updated : 30 Nov, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will see how to check the substring in a string. We are given two strings & we have to check whether the second string is a substring of the first string or not using PHP built-in strpos() function. This function is case-sensitive, which means that it treats upper-case and lower-case characters differently. Example: Consider the below example. Input :$s1 = "geeksforgeeks" $s2 = "for" Output : True Explanation: The 2nd string "for" is a substring of the 1st string "geeksforgeeks". So, the output is true. Input :$s1 = "practice.geeksforgeeks" $s2 = "quiz" Output : False Explanation: The 2nd string "quiz" is not a substring of the 1st string "practice.geeksforgeeks". So, the output is false.Approach: The problem can be solved by iterating through the given string from the 0th index to the final length of the string and comparing the query string with the iterations. If the index of the first occurrence of the given string is within the length indices of the given string then the output returns true, else the output returns false. In PHP, we can also make use of some built-in functions to solve this particular problem. These functions are: strpos(): This function finds the position of the first occurrence of a string inside another string.strlen() : It will returns length of string.Example: This example describes the comparison of the 2nd string with the 1st string & accordingly it will return true if the substring is present in it otherwise return false. PHP <?php // PHP code to check if a string is // substring of other $s1 = "geeksforgeeks"; $s2 = "geeks"; if (strpos($s1, $s2) >= 0 && strpos($s1, $s2) < strlen($s1)) echo("True"); else echo("False"); ?> Output: True Comment More infoAdvertise with us Next Article PHP to check substring in a string S Shubham_Singh_29 Follow Improve Article Tags : Misc Web Technologies PHP PHP-string Practice Tags : Misc Similar Reads How to check if a String Contains a Substring in PHP ? Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.MethodsBelow are the following methods by which we can check if a string contains a substri 1 min read How to get a substring between two strings in PHP? To get a substring between two strings there are few popular ways to do so. Below the procedures are explained with the example.Examples: Input:$string="hey, How are you?"If we need to extract the substring between "How" and "you" then the output should be are Output:"Are"Input:Hey, Welcome to Geeks 4 min read Check if String Contains Substring in Python This article will cover how to check if a Python string contains another string or a substring in Python. Given two strings, check whether a substring is in the given string. Input: Substring = "geeks" String="geeks for geeks"Output: yesInput: Substring = "geek" String="geeks for geeks"Output: yesEx 8 min read JavaScript - How To Check Whether a String Contains a Substring? Here are the different methods to check whether a string contains a substring in JavaScript.1. Using includes() MethodThe includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false oth 3 min read Javascript Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output 2 min read Check for Substring in JavaScript Given two strings, check if one string is substring of another."bce" is substring of "abcde""ae" is not substring of "abcde"Empty String is a substring of all stringsUsing includes() - Most used and Simplest MethodThe includes() method checks whether a string contains a substring. JavaScriptlet s = 2 min read How to replace String in PHP ? Replacing a string in PHP involves substituting parts of a string with another string. Common methods include str_replace() for simple replacements, preg_replace() for pattern-based replacements, substr_replace() for positional replacements, and str_ireplace() for case-insensitive replacements. Each 2 min read How to check a string starts/ends with a specific string in PHP ? In this article, we are going to learn how to check that a given string is starting or ending with a given specific string in PHP.We require the use of XAMPP, Netbeans to run this code.Input: "Geeks For Geeks"Starting with: "G"Output: "Geeks For Geeks Starts with String G"In PHP version 8+, there ar 2 min read Check if a given String is Binary String or Not using PHP Given a String, the task is to check whether the given string is a binary string or not in PHP. A binary string is a string that should only contain the '0' and '1' characters. Examples:Input: str = "10111001"Output: The string is binary.Input: str = "123456"Output: The string is not binary.Table of 5 min read How to check for empty string in PHP ? In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. We can use empty() function to check whether a string is empty or not.here we have some common approachesTable of ContentUsing empty() functio 4 min read Like