How to Extract Text Between Two Substrings In a String In PHP?

Solution of how to extract text between two substrings in a string.

Problem:

You want to extract the title text of a website. For this, you need a way that will extract the text between <title> and </title>.

Solution:

Example:

<?php
$string = file_get_contents('http://schoolsofweb.com/');
$start = '<title>';
$end = '</title>';

$startpos = strpos($string, $start) + strlen($start);
if (strpos($string, $start) !== false) {
    $endpos = strpos($string, $end, $startpos);
    if (strpos($string, $end, $startpos) !== false) {
        echo substr($string, $startpos, $endpos - $startpos);
    }
}
?>

Output:
Schools of Web – Teach Yourself Web Development and Web Designing Step by Step With Easy Explanation

How it Works:

Line 2 file_get_contents() function reads the entire content of the site http://schoolsofweb.com/ and stores it in the $string variable.
Line 3 Variable $start holds the first substring.
Line 4 Variable $end holds the second substring.
Line 6 strpos() function finds the position of the <title>(Sstart) in the $string then strlen() function gets the length of the <title> which is 7. Adding these two numbers, we get a number which is the starting position of the text inside the title tags.
Line 7 The if condition checks if the <title> exists in the string. If <title> tag doesn’t exist in $string, strpos($string, $start) returns false.
Line 8 Here, strpos() function finds the position of the </title>(Sstart) after the end of the first substring($startpos).  Variable $endpos holds that position.
Line 9 The if condition checks if the </title> exists in the string. If </title> tag doesn’t exist in $string, strpos($string, $end, $startpos) returns false.
Line 10 sunstr() function returns the text between the <title> and </title> which is ($endpos – $startpos) from the $string beginning from the starting position of the of the text inside the title tags ($startpos)

strpos() function at a glance

Usages:
strpos() function finds the position of the first occurrence of a substring in a string and returns that number.

Syntax:
strpos(search_into, search_for, start_from)

Parameters:

Parameter What it does
search_into Required Specify the string to be searched.
search_for Required Specifies the string to search.
start_from Optional Specify the character number from where the search will start.

String position starts at 0, not 1.

This function returns Boolean FALSE if there is no match found, but it doesn’t return TRUE.

Example :

<?php
$search_into = "Web Design Graphics Design Print Design.";
$search_for   = "Design";
$position = strpos($search_into, $search_for);

if($position !== false){
    echo "The string $search_for was found in the position $position.";
}else{
    echo "The string was not found.";
}
?>

Output:
The string ‘Design’ was found in the position 4.

Explanation:

Line 2 Variable $search_into holds the string to be searched.
Line 3 Variable $search_for holds the string to search.
Line 4 The function finds the position number 4 as the first occurrence of the string “Design” in the $search_into string. Though there are few more “Design” exist in the string, but the function ignore as soon as it finds the first match.
Line 6 The !== in the if condition ensures that the match found. As mentioned before that Boolean false is used to indicate that the match is not found.
Line 7 It prints the output.

We used Not Identical Operator !== to indicate that both values and types are not equal.

To check if the substring is at the beginning of the string, use === 0 which shows identical in both value and type. See the next example.

Example:


<?php
$search_into = "Web Design Graphics Design Print Design.";
$search_for   = "Web";
$position = strpos($search_into, $search_for);

if($position === 0){
    echo "The substring is at beginning.";
}else{
    echo "The substring is not at beginning";
}
?>

Output:
The substring is at beginning.

As the function finds a substring into a string, so if you want to find an integer, you need to convert it to a string at first before the function, otherwise it will not provide the right result. See the next example.

Example:

<?php
$search_into = "1234567";
$search_for   = 12;
$position = strpos($search_into, $search_for);

if($position === false){
    echo "Substring not found.";
}else{
    echo "Substring found at position $position.";
}
?>

Output:
Substring not found.

Explanation:

Line 3 Variable $search_for is an integer which shouldn’t be.

Now, let’s rewrite the function converting the number to string at first, then search with it-

<?php
$search_into = "1234567";
$search_for   = (string)12;
$position = strpos($search_into, $search_for);

if($position === false){
    echo "Substring not found.";
}else{
    echo "Substring found at position $position.";
}
?>

Output:
Substring found at position 0.

Explanation:

Line 3 The number is converted to string by type casting
Line 4 The function finds the match at the beginning(position 0)

More Examples:

Example 1: (How to find all the positions of a substring in a string, not only the first occurrence)

<?php
$search_into="Web Design Graphics Design Print Design.";
$search_for="Design";
$position=0;

while(($position=strpos($search_into,$search_for,$position))!==false){
    $position_array[]=$position;
}
echo "The word 'Design' Found in position: <br />";
foreach($position_array as $value) echo $value."<br />";
?>

Output:
The word ‘Design’ Found in position:
4
20
33

Explanation:

Line 6 The while loop starts searching the string from the first character of it(at line 4, the position sets to 0) and continues to the end. When the strpos() function finds any matches (that is !== becomes true, then the loop saves the position number in the array $position_array in line 7.
Line 4 The foreach loop prints the position number.


Pin It

↑ Back to Top