Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Regex find text between custom added HTML Tags

Tags:

regex

php

I have he following scenario:

Got an HTML template file that will be used for mailing.

Here is a reduced example:

    <table>
<tr>
<td>Heading 1</td>
<td>heading 2</td>
</tr>
<PRODUCT_LIST>
<tr>
<td>Value 1</td>
<td>Value 2</td>
</tr>
</PRODUCT_LIST>
</table>

All I need to do is to get the HTML code inside <PRODUCT_LIST> 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!

like image 325
Bathan Avatar asked Aug 30 '10 16:08

Bathan


1 Answers

Assuming <PRODUCT_LIST> tags will never be nested

preg_match_all('/<PRODUCT_LIST>(.*?)<\/PRODUCT_LIST>/s', $html, $matches);

//HTML array in $matches[1]
print_r($matches[1]);
like image 162
MooGoo Avatar answered Oct 16 '22 20:10

MooGoo