2015-09-15 59 views
0

我有一個擦除表格的函數。該表的<td>有很多值,當然這是不同的。因此,我需要一個或兩個以上的字符組合,以便無論數值如何都可以得到所有內容。未知文本的字符組合

function scrape_between($data, $start, $end){ 
    $data = stristr($data, $start); // Stripping all data from before $start 
    $data = substr($data, strlen($start)); // Stripping $start 
    $stop = stripos($data, $end); // Getting the position of the $end of the data to scrape 
    $data = substr($data, 0, $stop); // Stripping all data from after and including the $end of the data to scrape 
    return $data; // Returning the scraped data from the function 
} 

$match = $this -> scrape_between($array, '<td (__MAYBE SOME CHARACTER TO GET EVERYTHING NO MATTER WHAT__) class="V1_c01">', "</td>"); 

編輯:我想做一個foreach,因爲表有不同的ID,所以我想在國外搜索這些。

foreach ($separate_results as $key => $separate_result) { 
     if ($separate_result != "") { 
      $table[$key][0]= $this -> scrape_between($separate_result, '<td id="indhold_0_indholdbredvenstre_0_integrationwrapper_1_ctl01_Program_ProgramNormal_Program1_c04_0" class="V1_c04">', "</td>"); 
     } 
    } 
+1

你的問題不清楚。請添加輸入示例和所需輸出 – FuzzyTree

+0

也許DOM解析可以幫助您? http://php.net/manual/de/book.dom.php – Reeno

回答

1

如果擔心__SOMETHING HERE__如果類名V2_c01是固定的,那麼下面就是我的POC

<?php 
function scrape_between($data, $classname, $tagname){ 

    // get anything between `<td` and `classname` whereas `<td` must be the first occurence to the left of `classname` 
    $openstart = stristr(strrev($data), strrev($classname)); 
    $openstart = substr($openstart, strlen($classname)); 
    $openstart = substr($openstart, 0, stripos($openstart, '<'.$tagname)); 
    $openstart = strrev($openstart); 

    // get anything between `classname` and `>` whereas `>` must be the first occurence to the right of `classname` 
    $openend = stristr($data, $classname); 
    $openend = substr($openend, strlen($classname)); 
    $openend = substr($openend, 0, stripos($openend, '>')+1); 

    $start = $openstart.$classname.$openend; // '<td __SOMETHING HERE__ class="' . 'V1_c01' . '">' 
    $end = "</".$tagname.">"; 

    $data = stristr($data, $start); // Stripping all data from before $start 
    $data = substr($data, strlen($start)); // Stripping $start 
    $stop = stripos($data, $end); // Getting the position of the $end of the data to scrape 
    $data = substr($data, 0, $stop); // Stripping all data from after and including the $end of the data to scrape 
    return $data; // Returning the scraped data from the function 
} 

$array = '<table><tr><td>&nbsp;</td></tr><tr><td style="" id="td1"><table><tr><td style="" class="V1_c01" id="mytd">my td content</td></tr></table></td></tr><tr><td>&nbsp;</td></tr></table>'; 
$match = scrape_between($array, 'V1_c01', "td"); 

echo $match; 
echo '<br />'; 

$array = '<table><tr><td>&nbsp;</td></tr><tr><td style="" id="td1"><table><tr><td><span style="" class="V1_c01" id="myspan">my span content</span></td></tr></table></td></tr><tr><td>&nbsp;</td></tr></table>'; 
$match = scrape_between($array, 'V1_c01', "span"); 

echo $match; 

?> 

結果一:

my td content

結果二:

my span content

+0

哇,完美的工作! –