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 to extracting substrings from a string in PHP.
Table of Content
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
$str = "Welcome to GeeksforGeeks";
$subStr = substr($str, 11, 13);
echo $subStr;
?>
Output
GeeksforGeeks
Approach 2: Extracting Substring Before a Specific Character
To extract a substring that appears before a specific character or substring, you can use the strstr() function.
<?php
$str = "Welcome to GeeksforGeeks";
$subStr = strstr($str, 'G');
echo $subStr;
?>
Output
GeeksforGeeks
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
$str = "Welcome to GeeksforGeeks - A computer science portal";
if (preg_match('/to (.*?) -/', $str, $matches)) {
echo $matches[1];
}
?>
Output
GeeksforGeeks
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
// 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;
?>
Output
is
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
// 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