1

I have a html file that contains this table row:

<tr> 
<td class="color21 right" style="font-size:12px; line-height:1.2;">&nbsp;Location</td>
<td class="color21" style="font-size:12px;">10</td>
<td class="color21" style="font-size:12px;"><img src="../../icons/9.gif" alt="Type" />     </td>
<td class="color21" style="font-size:12px;">3</td>
<td class="color21" style="font-size:12px;">7</td>
<td class="color21" style="font-size:12px;"><img src="../../icons/11.gif" alt="Type" />    </td>
<td class="color21" style="font-size:12px;">3</td>
<td class="color21" style="font-size:12px;">10</td>
<td class="color21" style="font-size:12px;"><img src="../../icons/9.gif" alt="Type" />    </td>
</tr>

I'm retrieving file contents using file_get_contents.

How can I extract all TD values using preg_match, preg_match_all?

2 Answers 2

1

Use the DomParser to Parse the html content regex are not reliable on this cases.

    $str=file_get_contents('read.txt');
    $dom = new domDocument;
    $dom->loadHTML($str);
    $tr = $dom->getElementsByTagName('td');
    foreach($tr as $td)
  {
    if(!empty($td->nodeValue)){
        echo $td->nodeValue."\n";
    }else{
        $images=$td->getElementsByTagName('img');
        foreach($images as $image){
            echo $image->getAttribute('src')." ";
            echo $image->getAttribute('alt');
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

This works great for numeric values, but I also need image values such as 9.gif, 11.gif... Do you have any suggestion?
I would be satisfied to get whole img string along with numerical values <img src="../../icons/9.gif" alt="Type" />
@mpet i have edited my answer to get the img src along with the values.
1

Think over if you really wanna a regex to parse html

But you can use this:

<td.+?>(.+?)</td>

The first group will contain the values of <td>

1 Comment

What do you suggest instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.