假设我已经接近2000行,如下所示:
<div style="position:absolute;top:461;left:167"><nobr>1 001 A NAME HERE</nobr></div>
<div style="position:absolute;top:461;left:682"><nobr>TRUE</nobr></div>
<div style="position:absolute;top:480;left:167"><nobr>2 002 ANOTHER NAME GOES HERE</nobr></div>
<div style="position:absolute;top:480;left:682"><nobr>FALSE</nobr></div>
我希望将自动传递给表格,如下所示:
<tr>
<td class="id">1</td>
<td class="serial">001</td>
<td class="name">A NAME HERE</td>
<td class="accepted">TRUE</td>
</tr>
<tr>
<td class="id">2</td>
<td class="serial">002</td>
<td class="name">ANOTHER NAME GOES HERE</td>
<td class="accepted">FALSE</td>
</tr>
你能帮我解决一下如何用PHP / Javascript做到这一点吗?
PS:与定义的样式无关,因为它在表格中会更容易。
答案 0 :(得分:1)
假设$ str包含您的输入DIV:
<?php
preg_match_all('#<nobr>(?P<id>\d)(\s)(?P<number>\d{3})(\s)(?P<name>.*)</nobr>#Ui', $str, $names, PREG_SET_ORDER);
preg_match_all('#<nobr>(?P<bool>TRUE|FALSE)</nobr>#Ui', $str, $bools, PREG_SET_ORDER);
foreach($names as $i=>$row){
echo '<tr>
<td class="id">'.$row['id'].'</td>
<td class="serial">'.$row['number'].'</td>
<td class="name">'.$row['name'].'</td>
<td class="accepted">'.$bools[$i]['bool'].'</td>
</tr>'. PHP_EOL;
}
?>
请注意,此代码不受行结尾格式化的影响,因为它使用正则表达式。
答案 1 :(得分:0)
获取php变量中的所有数据;
$data = file_get_contents('temp.html');
$data_filter = explode('<nobr>',$data);
$final_data = array();
foreach($data_filter as $df1)
{
$temp = explode('</nobr>',$df1);
$final_data[] = $temp[0];
}
// now all your required data is in an array in sequences of two
$str = '';
while(count($final_data))
{
$name = array_pop($final_data);
while(strlen($name)<5){
$name = array_pop($final_data);
}
$accepted = array_pop($final_data);
$name = explode(' ',$name);
$str.= '<tr>
<td class="id">'.$name[0].'</td>
<td class="serial">'.$name[1].'</td>
<td class="name">'.$name[2].'</td>
<td class="accepted">'.$accepted.'</td>
</tr>
';
}
echo $str;