PHP Program To Check If A String Is Substring Of Another Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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: 5Explanation: String "for" is present as a substring of s2.Input: s1 = "practice", s2 = "geeksforgeeks"Output: -1.Explanation: There is no occurrence of "practice" in "geeksforgeeks"Simple Approach The idea is to run a loop from start to end and for every index in the given string check whether the sub-string can be formed from that index. This can be done by running a nested loop traversing the given string and in that loop run another loop checking for sub-string from every index. For example - consider there to be a string of length N and a substring of length M. Then run a nested loop, where the outer loop runs from 0 to (N-M) and the inner loop from 0 to M. For very index check if the sub-string traversed by the inner loop is the given sub-string or not. PHP <?php // PHP program to check if a // string is substring of other. // Returns true if s1 is substring // of s2 function isSubstring($s1, $s2) { $M = strlen($s1); $N = strlen($s2); // A loop to slide // pat[] one by one for ($i = 0; $i <= $N - $M; $i++) { $j = 0; // For current index i, // check for pattern match for (; $j < $M; $j++) if ($s2[$i + $j] != $s1[$j]) break; if ($j == $M) return $i; } return -1; } // Driver Code $s1 = "for"; $s2 = "geeksforgeeks"; $res = isSubstring($s1, $s2); if ($res == -1) echo "Not present"; else echo "Present at index " . $res; ?> OutputPresent at index 5Complexity Analysis: Time complexity: O(m * n) where m and n are lengths of s1 and s2 respectively. A nested loop is used the outer loop runs from 0 to N-M and inner loop from 0 to M so the complexity is O(m*n).Space Complexity: O(1). As no extra space is required.An efficient solution is to use a O(n) searching algorithm like KMP algorithm, Z algorithm, etc. Please refer complete article on Check if a string is substring of another for more details! Comment More infoAdvertise with us Next Article PHP Program To Check If A String Is Substring Of Another kartik Follow Improve Article Tags : Strings Pattern Searching Web Technologies PHP Programs DSA +1 More Practice Tags : Pattern SearchingStrings Similar Reads PHP Program to Check if Two Strings are Same or Not Given two strings, the task is to check whether two strings are same or not in PHP. In this article, we will be looking at different approaches to check two strings are same or not. Examples: Input: str1 = "Geeks", str2 = "Geeks" Output: Both Strings are the same Input: str1 = "Hello", str2 = "Geeks 2 min read How to Get All Substrings of a Given String in PHP ? Extracting all possible substrings from a given string is a common task in text processing, pattern recognition, and various other applications in PHP programming. This operation involves generating every possible combination of characters present in the string, including both contiguous and non-con 3 min read PHP Program to check if strings are rotations of each other or not Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false) Algorithm:areRotations(str1, str2): 1. Create a temp string and store concatenation of str1 to str1 in temp. temp = 2 min read PHP Program to Check if a Given String is Pangram or Not A pangram is a sentence that contains every letter of the alphabet at least once. Checking whether a given string is a pangram is a common programming task. In this article, we will explore different approaches to creating a PHP program that checks if a given string is a pangram or not.Table of Cont 4 min read How to Extract Substring from a String in PHP? Given a String, the task is to extract a substring from a string in PHP. Extracting substrings from a string is a common task in PHP, whether you need to extract a portion of text based on a specific position or find a substring based on a pattern. In this article, we will explore various approaches 3 min read PHP Program to Split & Join a String Given a String, the task is to split and join strings in PHP. It is a common operations that allow you to manipulate and manage text data effectively. Below are the approaches to split and join a string in PHP: Table of Content Using explode() and implode() FunctionUsing str_split() and Custom Join 3 min read PHP to Check if a String Contains any Special Character Given a String, the task is to check whether a string contains any special characters in PHP. Special characters are characters that are not letters or numbers, such as punctuation marks, symbols, and whitespace characters. Examples: Input: str = "Hello@Geeks"Output: String contain special character 3 min read How to remove portion of a string after a certain character in PHP? In PHP, removing a portion of a string after a certain character refers to trimming the string by locating a specific character and then eliminating everything that follows it. This technique is useful for extracting substrings or cleaning data by removing unwanted parts.Here we are some common appr 2 min read How to get the last n characters of a PHP string? Write a PHP program to get last n characters of a given string. Examples: Input : $str = "GeeksforGeeks!" $n = 6 Output : Geeks! Input : $str = "GeeksforGeeks!" $n = 9 Output : forGeeks! Method 1: Tn this method, traverse the last N characters of the string and keep appending them in a new string. E 1 min read PHP Program to Check if a String Contains Uppercase, Lowercase, Special Characters and Numeric Values Given a String, the task is to check whether the given string contains uppercase, lowercase, special characters, and numeric values in PHP. When working with strings in PHP, it's often necessary to determine the presence of certain character types within the string, such as uppercase letters, lowerc 3 min read Like