How to Extract Content Between HTML Tags using PHP

The preg_match() function is the best option to extract text between HTML tags with REGEX in PHP. If you want to get content between tags, use regular expressions with preg_match() function in PHP. You can also extract the content inside element based on class name or ID using PHP.

The example code snippet shows how to get the content inside the specific div (HTML element) using PHP. The following code uses preg_match() with the regular expression to extract text or HTML content from under 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 content including the parent element (tags):

$match[0];

Extract and returns the content between tags:

$match[1];

5 Comments

  1. Victor Said...
  2. Ehsan Said...
  3. CR76 Said...
  4. Radan Said...
  5. Harry Said...

Leave a reply

keyboard_double_arrow_up