Welcome to WebmasterWorld Guest from 54.145.65.62

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

preg match all value between two tags and loop

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

Junior Member

10+ Year Member

joined:July 17, 2006
posts:137
votes: 0


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

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

Junior Member

10+ Year Member

joined:Apr 22, 2005
posts:185
votes: 0


$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]

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

Junior Member

10+ Year Member

joined:Apr 22, 2005
posts:185
votes: 0


# 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];
}
}