Welcome to WebmasterWorld Guest from 54.160.131.144

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

preg match all value between two tags and loop

     

ahmed24

2:21 pm on Dec 4, 2009 (gmt 0)

5+ Year Member



I have a variable called $content that looks like this:

$content = '<td class="data2">1st Information</td><td class="data2">2nd Information</td><td class="data2">3rd Information</td>';

i want to be able to get the information between the <td class="data2"> and the </td> tag and loop it.

So for example once it is done, instead of having the following:

<td class="data2">1st Information</td>
<td class="data2">2nd Information</td>
<td class="data2">3rd Information</td>

It should be the following:

1st Information<br>
2nd Information<br>
3rd Information<br>

Can anyone please help?

Thanks

NomikOS

3:28 pm on Dec 4, 2009 (gmt 0)

10+ Year Member



$content = '<td class="data2">1st Information</td><td class="data2">2nd Information</td><td class="data2">3rd Information</td>'; 
$store = array();

if (preg_match_all("/<td class=\"data2\">(.*?)<\/td>/si", $content, $aux))
{
$store[] = $aux[0];
}

var_dump($store);

[edited by: NomikOS at 3:34 pm (utc) on Dec. 4, 2009]

NomikOS

3:29 pm on Dec 4, 2009 (gmt 0)

10+ Year Member



# to isolate each data cell use PREG_SET_ORDER flag: 

if (preg_match_all("/<td class=\"data2\">(.*?)<\/td><td class=\"data2\">(.*?)<\/td><td class=\"data2\">(.*?)<\/td>/si", $content, $aux, PREG_SET_ORDER))
{
foreach ($aux as $aux2)
{
$store['1st Information'][] = $aux2[1];
$store['2st Information'][] = $aux2[2];
$store['3st Information'][] = $aux2[3];
}
}