How to extract content between html tags in 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 = 'Content goes here...
';preg_match['/[.*?]/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 he following scenario:
Got an HTML template file that will be used for mailing
.
Here is a reduced example:
Heading 1 |
heading 2 |
Value 1 |
Value 2 |
All I need to do is to get the HTML code inside
and then repeat that code as many times as products I have on an array.
What
would be the right PHP Regex code for getting/replacing this List?
Thanks!
Sumit patel
3,5358 gold
badges31 silver badges57 bronze badges
asked Aug 30, 2010 at 16:26
1
Assuming
tags will never be nested
preg_match_all['/[.*?]/s', $html, $matches];
//HTML array in $matches[1]
print_r[$matches[1]];
answered Aug 30, 2010 at 16:38
MooGooMooGoo
45k4
gold badges38 silver badges32 bronze badges
2
Use
Simple HTML DOM Parser. It's easy to understand and use .
$html = str_get_html[$content];
$el = $html->find['PRODUCT_LIST', 0];
$innertext = $el->innertext;
answered Aug 30, 2010 at 16:34
webbiedavewebbiedave
47.7k8 gold badges87 silver badges100 bronze badges
0
Use this function. It will return all found values as an array.