How to Extract data Between HTML Tags in PHP


The preg_match() function is the easy choice to extract data between HTML tags in php with REGEX using PHP. If you want to get data between tags, use regular expressions with preg_match() function using PHP. You can also extract the data within element based on class/ID in PHP.

The sample code explain how to get the data within the particular div (HTML element) in PHP. The given code uses preg_match() with the regular expression to get text or HTML content from within tags using PHP.

$htmlContent = '<div class="my-con">Content goes here...</div>';

preg_match('/<div class="my-con">(.*?)<\/div>/s', $htmlContent, $match);

Extract and returns the data, including the parent element (tags):


Extract and returns the content between tags:


Also Read: How to send SMS with PHP?

Like it? Share with your friends!