How to extract data/content between HTML Tags in PHP

I am explaining you, how to extract data/content between HTML Tags in PHP.
preg_match() function provide functionality to extract text between HTML tags with REGEX in PHP. we can get content between tags with the use of regular expressions with preg_match() function in PHP.

Example :

<?php
  $htmlwithtext = '<div class="checkdata">This is webdeveloperindia group.</div>';
  preg_match('/<div class="checkdata">(.*?)<\/div>/s', $htmlwithtext, $matchdata);
  echo "<br>";
  echo $matchdata[0];
  echo "<br>";
  echo $matchdata[1];
?>

we can find result will be stored in $matchdata variable. if you will run the code then you should inspect the element to see the difference between $matchdata[0] and $matchdata[1].

Extract the content, including the parent html element tags:

echo $matchdata[0];

Output :

<div class="checkdata">This is webdeveloperindia group.</div>

Extract the content between html tags:

echo $match[1];

Output :

This is webdeveloperindia group.

One thought to “How to extract data/content between HTML Tags in PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *

22 − 16 =