0

假设我已经有近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:定义的样式无关紧要,因为在表格中会更容易。

4

4 回答 4

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;
}
?>

请注意,此代码不受行尾格式的影响,因为它使用正则表达式。

于 2012-07-27T13:45:58.077 回答
1

这是一个javascript版本。

    <script>
        var data = [];

        $('div').each(function(index) {
            data[index] = $(this).text();
        });

        var str = '';

        for (var i=0; i < data.length; i=i+2) {
            var temp = data[i].split(' ');
            var name = temp.slice(2).join(' ');
            str += '<tr>' +
                        '<td class="id">'+ temp[0] + '</td>' + 
                        '<td class="serial">' + temp[1] + '</td>' +
                        '<td class="name">' + name +'</td>' +
                        '<td class="accepted">' + data[i + 1] + '</td>' +
                    '</tr>';
            temp = [];
        }

        console.log(str);
    </script>

str 变量包含您的表数据。只需将该数据添加到您的 html 页面。

于 2012-07-27T13:48:24.183 回答
0

$string是所有div的字符串。假设换行符是 \r\n,将所有这些更改为正确的换行符

$sections = explode("\r\n", strip_tags($string));//Remove divs ad nobrs and sort into lines
$numSections = count($sections);
for($row = 0; $row < $numSections; $row += 2) {
    $rows[0] = explode(" ", $sections[$row]);
    $rows[1] = $sections[$row + 1];?>
<tr>
    <td class="id"><?php echo $rows[0][0]; ?></td>
    <td class="serial"><?php echo $rows[0][1]; ?></td>
    <td class="name"><?php for($i=2; $i<count($rows[0]); $i++) echo $rows[0][$i]+" "; ?></td>
    <td class="accepted"><?php echo $rows[1]; ?></td>
</tr>

<?php  }

假设您希望它回显

于 2012-07-27T13:23:44.853 回答
0

lat 在 temp.html 中说你当前的 html

获取 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;
于 2012-07-27T13:30:55.920 回答