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('/(.*?)<\/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 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!

How to extract content between html tags in php?

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('/(.*?)<\/PRODUCT_LIST>/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.

', '');

answered Aug 30, 2010 at 16:34

shamittomarshamittomar

45.4k12 gold badges74 silver badges78 bronze badges

4

as above is ok but with performance is really horrible If You can use PHP 5 you can use DOM object like this :

     loadXML($html);
    }
    else
    {
        $dom->loadHTML($html);
    }

    /*** discard white space ***/
    $dom->preserveWhiteSpace = false;

    /*** the tag by its tag name ***/
    $content = $dom->getElementsByTagname($tag);

    /*** the array to return ***/
    $out = array();
    foreach ($content as $item)
    {
        /*** add node value to the out array ***/
        $out[] = $item->nodeValue;
    }
    /*** return the results ***/
    return $out;
}
?>

and after adding this function You can just use it as:

$content = getTextBetweenTags('PRODUCT_LIST', $your_html);

foreach( $content as $item )
{
    echo $item.'
'; } ?>

yep, i just learn this today. dont use preg for html with php5

answered Sep 12, 2014 at 21:37

fearisfearis

3842 silver badges14 bronze badges

try this regular expression in preg match all function

(.*?)<\/PRODUCT_LIST>

How to extract content between html tags in php?

Rohan Kumar

40k11 gold badges73 silver badges103 bronze badges

answered Aug 30, 2010 at 17:39