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];
I have been working for hours trying to capitalise all heading (h1-h6) elements in my wordpress post.
I will appreciate any help I can get.
How can we extract the text between all H2 tags with whatever attributes they have (like id , class) from an html ?
@Radan:
Perhaps take a look at preg_match_all
Hello.
What if I had multiple divs with the same class and I would like to get all of them?
This code only gets the first one. Thank you.
after searching for n number of hours. I always finish on this website for quality content. Thanks a lot for posting quality content