PHP Find the number of sub-string occurrences Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report We are given two strings s1 and s2. We need to find the number of occurrences of s2 in s1. Examples: Input : $s1 = "geeksforgeeks", $s2 = "geeks" Output : 2 Explanation : s2 appears 2 times in s1 Input : $s1 = "Hello Shubham. how are you?"; $s2 = "shubham" Output : 0 Explanation : Note the first letter of s2 is different from substrings present in s1. The problem can be solved using PHP inbuilt function for counting the number of occurrences of a substring in a given string. The in-built function used for the given problem is: substr_count()The substr_count() function counts the number of times a substring occurs in a string. Note: The substring is case-sensitive. PHP <?php // PHP program to count number of times // a s2 appears in s1. $s1 = "geeksforgeeks"; $s2 = "geeks"; $res = substr_count($s1, $s2); echo($res); ?> Output : 2Using preg_match_all() FunctionThe preg_match_all() function searches a string for all matches to a given pattern and returns the number of matches.Example PHP <?php function countOccurrences($s1, $s2) { // Escape special characters in $s2 to safely use it in a regex pattern $pattern = '/' . preg_quote($s2, '/') . '/'; // Perform a global regular expression match preg_match_all($pattern, $s1, $matches); // Return the number of matches found return count($matches[0]); } $s1 = "geeksforgeeks"; $s2 = "geeks"; echo "Number of occurrences of '$s2' in '$s1': " . countOccurrences($s1, $s2) . PHP_EOL; $s1 = "Hello Shubham. how are you?"; $s2 = "shubham"; echo "Number of occurrences of '$s2' in '$s1': " . countOccurrences($s1, $s2) . PHP_EOL; ?> Output:Number of occurrences of 'geeks' in 'geeksforgeeks': 2 Number of occurrences of 'shubham' in 'Hello Shubham. how are you?': 0 Comment More infoAdvertise with us Next Article PHP Find the number of sub-string occurrences S Shubham_Singh_29 Follow Improve Article Tags : Misc Web Technologies PHP PHP-string Practice Tags : Misc Similar Reads Number of substrings of one string present in other Suppose we are given a string s1, we need to the find total number of substring(including multiple occurrences of the same substring) of s1 which are present in string s2. Examples: Input : s1 = aab s2 = aaaab Output :6 Substrings of s1 are ["a", "a", "b", "aa", "ab", "aab"]. These all are present i 4 min read PHP to check substring in a string 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 lo 2 min read PHP program to find the length of the last word in string We are given a string. We are required to write a program in PHP to find the length of the last word in the string using inbuilt functions. We have already discussed approach of solving this problem here. This article discusses the PHP solution to the problem. Examples: Input : "php exercises" Outpu 2 min read 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 the last character of a string in PHP ? In this article, we will find the last character of a string in PHP. The last character can be found using the following methods.Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in 2 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 PHP Different characters in the given string Different characters in a given string refer to the unique, non-repeating characters that appear within the string. Identifying these characters involves recognizing each distinct character while excluding any duplicates. This concept is useful in various programming and data analysis tasks where un 2 min read Concatenation of two strings in PHP In this article, we will concatenate two strings in PHP. There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right 2 min read Frequency of a Substring in a String Given an input string and a pattern, the task is to find the frequency of occurrences of the string pattern in a given string. Examples: Input: pattern = "man", string = "dhimanman"Output: 2 Input: pattern = "nn", string = "Banana"Output: 0 Input: pattern = "aa", string = "aaaaa"Output : 4 Recommend 14 min read How to repeat a string to a specific number of times in PHP ? A string is a sequence of characters stored in PHP. The string may contain special characters or numerical values or characters. The strings may contain any number of characters and may be formed by the combination of smaller substrings. Table of ContentUsing for loopUsing str_repeat methodUsing Rec 3 min read Like