How to Extract Substring from a String in PHP? Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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 to extracting substrings from a string in PHP.Table of ContentUsing substr() FunctionExtracting Substring Before a Specific CharacterUsing Regular Expressions (Regex)Using explode() FunctionUsing mb_substr() FunctionApproach 1: Using substr() FunctionThe substr() function is used to extract a substring from a string in PHP. It takes three arguments: the input string, the starting position (zero-based index), and optionally the length of the substring to extract. PHP <?php $str = "Welcome to GeeksforGeeks"; $subStr = substr($str, 11, 13); echo $subStr; ?> OutputGeeksforGeeksApproach 2: Extracting Substring Before a Specific CharacterTo extract a substring that appears before a specific character or substring, you can use the strstr() function. PHP <?php $str = "Welcome to GeeksforGeeks"; $subStr = strstr($str, 'G'); echo $subStr; ?> OutputGeeksforGeeksApproach 3: Using Regular Expressions (Regex)Regex provides a powerful way to extract substrings based on patterns. The preg_match() function can be used to extract substrings that match a specific regex pattern. PHP <?php $str = "Welcome to GeeksforGeeks - A computer science portal"; if (preg_match('/to (.*?) -/', $str, $matches)) { echo $matches[1]; } ?> OutputGeeksforGeeksApproach 4: Using explode() FunctionThe explode() function in PHP is used to split a string into an array based on a specified delimiter. By splitting the string and then accessing the desired element of the resulting array, you can effectively extract a substring.Example: In this example, the string is split into an array of words using the space character as the delimiter. The third word (with zero-based index 2) is then accessed and printed. PHP <?php // Sample string $string = "Hello, this is an example string."; // Split the string into an array using a space as the delimiter $array = explode(" ", $string); // Accessing the desired element $substring = $array[2]; // Extracting "is" echo $substring; ?> OutputisUsing mb_substr() FunctionThe mb_substr() function is used to extract a substring from a string, similar to the substr() function but specifically designed for multibyte character encodings. This function takes three arguments: the input string, the starting position (zero-based index), and optionally the length of the substring to extract.Example PHP <?php // Sample string $string = "Hello, this is an example string."; // Function to extract a word by its position using mb_substr function getWordAtPosition($string, $wordPosition) { // Split the string into an array of words $words = explode(" ", $string); // Calculate the start position of the desired word $startPosition = 0; for ($i = 0; $i < $wordPosition; $i++) { $startPosition += mb_strlen($words[$i]) + 1; // +1 for the space delimiter } // Extract the desired word using mb_substr $wordLength = mb_strlen($words[$wordPosition]); $substring = mb_substr($string, $startPosition, $wordLength); return $substring; } // Extract the third word (zero-based index 2) $substring = getWordAtPosition($string, 2); // Extracting "is" echo $substring; ?> Outputis Comment More infoAdvertise with us Next Article How to Extract Substring from a String in PHP? B blalverma92 Follow Improve Article Tags : PHP PHP-string Similar Reads How to extract Numbers From a String in PHP ? Extracting numbers from a string involves identifying and isolating numerical values embedded within a text. This process can be done using programming techniques, such as regular expressions, to filter out and retrieve only the digits from the string, ignoring all other characters.Here we have some 3 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 How to Substring a String in Python A String is a collection of characters arranged in a particular order. A portion of a string is known as a substring. For instance, suppose we have the string "GeeksForGeeks". In that case, some of its substrings are "Geeks", "For", "eeks", and so on. This article will discuss how to substring a str 4 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 How to Extract Words from given String in PHP ? Extracting words from a given string is a common task in PHP, often used in text processing and manipulation. Below are the approaches to extract words from a given string in PHP: Table of Content Using explode() functionUsing regular expressionsUsing explode() functionIn this approach, we are using 1 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 Extract Characters from a String in R Strings are one of R's most commonly used data types, and manipulating them is essential in many data analysis and cleaning tasks. Extracting specific characters or substrings from a string is a crucial operation. In this article, weâll explore different methods to extract characters from a string i 4 min read How to extract the user name from the email ID using PHP ? Given a string email address, extract the username. Input: â[email protected]â Output: priyank Input: â[email protected]â Output: princepriyank Approach 1: Using PHP strstr() to extract the username from the email address. In this, â@â symbol is the separator for the domain name and user name of 2 min read How to read each character of a string in PHP ? A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHPTable of ContentUsing str_split() method - The str_ 4 min read How to get the position of character in a string in PHP ? In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function. Syntax: strpos(string, character, start_pos) Parameters: string (mandatory): This parameter refers t 2 min read Like