1

I'm trying to get values from a table in a webpage and for that I'm using Simple HTML Dom library. This is how my code looks like:

include('simple_html_dom.php');

$html = file_get_html('http://www.lvbp.com/posicion.html');

$arr = array();
foreach ($html->find('tr') as $e) {
    array_push($arr, $e->innertext);
}

echo '<pre>';
print_r($arr);
echo '</pre>';

for ($i = 2; $i < count($arr); $i++) {
    str_replace("", "-", $arr[$i]);
    print_r($arr[$i]);
}

And I get this as output when print_r($arr):

Array
(
    [0] =>       EQUIPOS      J      G      P      Vent    
    [1] => 
    [2] =>       Navegantes      11      8      3      0    
    [3] =>       Tigres      11      8      3      0    
    [4] =>       Caribes      11      6      5      2    
    [5] =>       Leones      11      6      5      2    
    [6] =>       Aguilas      11      5      6      3    
    [7] =>       Tiburones      10      4      6      3.5    
    [8] =>       Cardenales      10      3      7      4.5    
    [9] =>       Bravos      11      3      8      5    
)

But from here I need to get separately meaning "Navegantes", "11", "8" and so on ... for each array position. For that my last code:

for ($i = 2; $i < count($arr); $i++) {
    str_replace("", "-", $arr[$i]);
    print_r($arr[$i]);
}

But it's not working since I get this as result:

Navegantes 11 8 3 0 Tigres 11 8 3 0 Caribes 11 6 5 2 Leones 11 6 5 2 Aguilas 11 5 6 3 Tiburones 10 4 6 3.5 Cardenales 10 3 7 4.5 Bravos 11 3 8 5 

What I'm missing? Any help?

UPDATE

This is how my code looks like based on suggestions:

include('simple_html_dom.php');
$html = file_get_html('http://www.lvbp.com/posicion.html');

$arr = array();
foreach ($html->find('tr') as $e) {
    $narr = array();
    foreach ($e->find('td') as $vp) {
        array_push($narr, $vp->plaintext);
    }
    $arr[] = array($narr);
}
4
  • Use $arr[$i]=str_replace("", "-", $arr[$i]); instead of str_replace("", "-", $arr[$i]); Commented Oct 23, 2013 at 16:06
  • @Subin changed but get the same result, no dashes (-) between words Commented Oct 23, 2013 at 16:10
  • Are you trying to get each values of td in a tr as an array ? Commented Oct 23, 2013 at 16:12
  • @Subin, yes, this is exactly what I'm trying to do Commented Oct 23, 2013 at 16:14

2 Answers 2

1

Try this :

$arr = array();
foreach ($html->find('tr') as $e) {
 $narr=array();
 foreach($e->find('td') as $vp){
  array_push($narr,$vp->plaintext);
 }
 $arr[]=array($narr);
}

instead of :

foreach ($html->find('tr') as $e) {
    array_push($arr, $e->innertext);
}

And drop the code :

for ($i = 2; $i < count($arr); $i++) {
    str_replace("", "-", $arr[$i]);
    print_r($arr[$i]);
}

You will get an array with keys as tr tags and their values as each td of tr.

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

5 Comments

No, you're wrong I get this error: PHP Fatal error: Call to a member function find() on a non-object in /var/www/html/reader/parser/posicion.php on line 29, referer: http://devserver/reader/parser/ where line 29 is foreach($code->find('td') as $vp){ ...
Are you sure you replaced correctly ? Because on my side it's working.
Are you using the latest version of simple_html_dom ? simplehtmldom.sourceforge.net
Yes I'm downloaded few seconds ago
0

Here's an aproach:

// includes Simple HTML DOM Parser
include "simple_html_dom.php";

$url = "http://www.lvbp.com/posicion.html";

//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load_file($url);

// parse rows
foreach ($html->find('tr') as $i => $row) {

    // Skip the second empty row
    if ($i == 1)
        continue;

    // parse and print cells
    foreach ($row->find('td') as $j => $col) {
        echo $col->plaintext;
        echo "|";
    }
    echo "<hr>";
}


// Clear DOM object (needed essentially when using many)
$html->clear(); 
unset($html);

Live DEMO

Comments

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.