Search code examples
phpsimple-html-dom

PHP simplehtmldom find text in blank table structure


I'm having difficulty finding the DYNAMIC-TEXT value in a sea of HTML tables.

I have tried $html->find("th[plaintext*=Type") and from here, I wanted to access the sibling, but return nothing. Here's the table structure

<table>
    <tbody>
    </tbody>

    <colgroup>
        <col width="25%">
        <col>
    </colgroup>

    <tbody>
        <tr class="odd">
            <th colspan="2">Name</th>
        </tr>

        <tr class="even">
            <th width="30%">Type</th>
            <td>DYNAMIC-TEXT</td>
        </tr> 
    </tbody>
</table>

I expect the output to be the text of DYNAMIC-TEXT but the action output is nothing

Thanks


Solution

  • In your code $html->find("th[plaintext*=Type") you want to use an attribute selector *= but there is no attribute plaintext.

    But there is an attribute width with the value 30%. You might use a pattern ^[0-9]+%$ to check for 1+ digits followed by a percentage sign.

    If you find a result, you could get the next_sibling and get the plaintext from it.

    For example:

    $html = str_get_html($str);
    foreach ($html->find("th[width*=^[0-9]+%$]") as $value) {
        echo $value->next_sibling()->plaintext;
    }
    

    Result:

    DYNAMIC-TEXT