1

Lets say we have the following structure in a web page. How can i retrieve the following information using php? something XAXAXA-SASASASA 2-1 ZAZAZAZA-CACACACA 2-2

<th class='black' colspan='6'>something</th>
<tr class=''>
        <td bgcolor = '#272727' width = 40%>XAXAXA</td>
        <td bgcolor = '#272727'  width = '5%'> - </td>
        <td bgcolor = '#272727' width = '40%'>SASASASA</td>
        <td bgcolor = '#272727'  width = '5%'>2</td>
        <td bgcolor = '#272727'  width = '5%'> - </td>
        <td bgcolor = '#272727' width = '5%'>1</td>
    </tr>
<tr class=''>
        <td bgcolor = '#484848' width = 40%>ZAZAZAZA</td>
        <td bgcolor = '#484848'  width = '5%'> - </td>
        <td bgcolor = '#484848' width = '40%'>CACACACA</td>
        <td bgcolor = '#484848'  width = '5%'>2</td>
        <td bgcolor = '#484848'  width = '5%'> - </td>
        <td bgcolor = '#484848' width = '5%'>2</td>
    </tr>
Alec Gorge
  • 17,110
  • 10
  • 59
  • 71
Nidis
  • 165
  • 6
  • 13

3 Answers3

1

You can use DOMDocument for this, pretty simple:

$DOM = new DOMDocument();
$DOM->loadHTML($Content);
$TH = $DOM->getElementsByTagName("td");

foreach($TH as $Item)
{
    echo $Item->nodeValue;
}
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
-1

You can use a regular expression to match a pattern like that. The preg_match() function is a good place to start.

Dave Child
  • 7,573
  • 2
  • 25
  • 37
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – RobertPitt Feb 21 '11 at 00:58
-1

I hope you accept an answer this time! Just pull out the match groups 1 to 13.

$sourcestring="your source string";
preg_match_all('/<th[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*)/is',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);

Result:

    [0] => Array
        (
            [0] => <th class='black' colspan='6'>something</th>
<tr class=''>
        <td bgcolor = '#272727' width = 40%>XAXAXA</td>
        <td bgcolor = '#272727'  width = '5%'> - </td>
        <td bgcolor = '#272727' width = '40%'>SASASASA</td>
        <td bgcolor = '#272727'  width = '5%'>2</td>
        <td bgcolor = '#272727'  width = '5%'> - </td>
        <td bgcolor = '#272727' width = '5%'>1</td>
    </tr>
<tr class=''>
        <td bgcolor = '#484848' width = 40%>ZAZAZAZA</td>
        <td bgcolor = '#484848'  width = '5%'> - </td>
        <td bgcolor = '#484848' width = '40%'>CACACACA</td>
        <td bgcolor = '#484848'  width = '5%'>2</td>
        <td bgcolor = '#484848'  width = '5%'> - </td>
        <td bgcolor = '#484848' width = '5%'>2
        )

    [1] => Array
        (
            [0] => something
        )

    [2] => Array
        (
            [0] => XAXAXA
        )

    [3] => Array
        (
            [0] =>  - 
        )

    [4] => Array
        (
            [0] => SASASASA
        )

    [5] => Array
        (
            [0] => 2
        )

    [6] => Array
        (
            [0] =>  - 
        )

    [7] => Array
        (
            [0] => 1
        )

    [8] => Array
        (
            [0] => ZAZAZAZA
        )

    [9] => Array
        (
            [0] =>  - 
        )

    [10] => Array
        (
            [0] => CACACACA
        )

    [11] => Array
        (
            [0] => 2
        )

    [12] => Array
        (
            [0] =>  - 
        )

    [13] => Array
        (
            [0] => 2
        )
Highly Irregular
  • 38,000
  • 12
  • 52
  • 70