Luis
Luis

Reputation: 1097

Pass data and replace in Javascript / PHP

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.

Upvotes: 0

Views: 148

Answers (4)

user1028286
user1028286

Reputation:

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.

Upvotes: 1

meiamsome
meiamsome

Reputation: 2944

$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

Upvotes: 0

Dzhuneyt
Dzhuneyt

Reputation: 8701

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.

Upvotes: 1

Rupesh Patel
Rupesh Patel

Reputation: 3065

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;

Upvotes: 0

Related Questions