Search code examples
simple-html-dom

SimpleHTMLDom Library


I am using this library : http://simplehtmldom.sourceforge.net/manual.htm

I have the following page which I want to extract in each line:

<div class="amx-form-entry-container">
            <table style="font-size: 16px; color:#000">
                <tbody><tr>
                    <td>Submitted by:</td>
                    <td>
                       Rami
                    </td>
                </tr>
                <tr>
                    <td>
                        Assignee:
                    </td>
                    <td> </td>
                </tr>
                <tr>
                    <td>
                        Subcategory:
                    </td>
                    <td> H:  Documentation</td>
                </tr>
                <tr>
                    <td>
                        Description:
                    </td>
                    <td>Hello</td>
                </tr>
                <tr>
                    <td>
                        Case reference:
                    </td>
                    <td>
                        BEY/0517/4444

                    </td>
                </tr>
                <tr>
                    <td>
                       Status:
                    </td>
                    <td> Closed</td>
                </tr>
                <tr>
                    <td>
                        Created on:
                    </td>
                    <td> 19 May 2017</td>
                </tr>

            </tbody></table>
        </div>

For example:
Submited by : Rami
Assignee: ..
Description: Hello

I have done this code : $oDom = new simple_html_dom(); $dd=$oDom->load($result); $test= $oDom->find("div[class='amx-module amx-mr-form-panel']",0); echo $test;

But it prints the output as a table, If i view the page source I will see same above code. - What I want to achieve is print each element in one line.


Solution

  • Here is my code

    $oDom->load($result);
    $tables = $oDom->find('table');
    
    $theData = array();
    foreach($tables[0]->find('tr') as $row){
        $rowData = array();
        foreach($row->find('td') as $cell) {
            $rowData[] = $cell->innertext;
        }
        $theData[] = $rowData;
    }
    echo "<pre>"; print_r($theData); echo "</pre>";
    
    
    ### Result ###
    
    Array
    (
    [0] => Array
        (
            [0] => Submitted by:
            [1] => Rami                      
        )
    [1] => Array
        (
            [0] => Assignee:                      
            [1] =>  
        )
    [2] => Array
        (
            [0] => Subcategory:                      
            [1] => H:  Documentation
        )
    [3] => Array
        (
            [0] => Description:                      
            [1] => Hello
        )
    [4] => Array
        (
            [0] => Case reference:                      
            [1] => BEY/0517/4444                        
        )
    [5] => Array
        (
            [0] => Status:                      
            [1] => Closed
        )
    [6] => Array
        (
            [0] => Created on:                      
            [1] => 19 May 2017
        )
    )