How to Extract Substring from a String in PHP?
Last Updated :
22 Jul, 2024
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.
Approach 1: Using substr() Function
The 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;
?>
To 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;
?>
Approach 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];
}
?>
Approach 4: Using explode() Function
The 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;
?>
Using mb_substr() Function
The 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;
?>
Output
is
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