簡體   English   中英

使用正則表達式從HTML表中提取特定值

[英]Extract specific values from HTML table using regex

我有一個包含此表行的html文件:

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

我正在使用file_get_contents檢索文件內容。

如何使用preg_match,preg_match_all提取所有TD值?

考慮一下您是否真的想使用正則表達式來解析html

但是您可以使用以下命令:

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

第一組將包含<td>的值

在這種情況下,請使用DomParser解析html內容正則表達式。

    $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');
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM