0

Let's say I already have near 2000 lines like these:

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

and I want to pass them automatically into a table like here:

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

Can you help me on how can I do that with PHP/Javascript?

PS: It doesn't matter the style defined, since it will be easier in tables.

3
  • Tell us how your 2000 lines are generated ? Commented Jul 27, 2012 at 13:20
  • @JBRTRND I already have them, in an HTML file. Any suggestion? Commented Jul 27, 2012 at 13:23
  • So, I think you should use Javascript/jQuery to achieve what you want to do.(You don't have any way to store these lines in database ?) Commented Jul 27, 2012 at 13:23

4 Answers 4

1

Here is a javascript version.

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

The str variable contains your table data. Just add that data to your html page.

Sign up to request clarification or add additional context in comments.

2 Comments

It's not working at all, sorry but the data is getting the same and it also throws a lot of "undefined" strings.
Oww, sorry men! This worked just right now, it's the best answer ;)
1

Assuming $str contains your input DIVs:

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

Note that this code is not affected by how your line endings are formatted, because it uses regular expression.

1 Comment

I'm sorry but it throws... nothing.
0

lat say your curent html in temp.html

get all data in a php variable;

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

2 Comments

<tr> <td class="id">TRUE</td> <td class="serial"></td> <td class="name"></td> <td class="accepted">1 001 A NAME HERE</td> </tr> its not parsing correctly the data.
The same thing, but I thing I saw the error. The first line it pushes contents <tr> <td class="id"><div</td> <td class="serial">style="position:absolute;top:461;left:167"></td> <td class="name"></td> <td class="accepted"></td> </tr> so I thing you need to parse the style.
0

$string is the string of all the divs. Assumes linebreaks are \r\n, change all those to correct linebreaks

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

Assumes you wanted it echo'd

5 Comments

This is what I need, but just one question, can you see please again the question? There's no linebreak, someone edited in order to be more "clear".
Ah, I see. Give me a second and I'll have that fixed for you.
Sure, thanks. Also a little bit problem, in my $string I can't add those lines with the linebreaks, or yes? Because it throws Parse error: syntax error, unexpected '}' in ...
If that's the case it's an error with my code :P, try this one and comment the error if it comes out again!
It was throwing an Error: Wrong parameter count for explode() in (lines with $sections and $rows[0]), but I corrected the syntaxis and worked the explode function. Now I'm getting this <tr> <td class="id">1</td> <td class="serial">001</td> <td class="name">002 003 004...</td> <td class="accepted"></td> </tr>

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.