Search code examples
phpcellfile-get-contentsexplode

PHP get cell content


I want to retrieve with PHP the contents of a table cell to another file. With this code, I retrieve the contents of all cells of the row.

$url = 'folder/myPage.php';
$content = file_get_contents($url);
$first_step = explode('<tr id="foo">' , $content );
$second_step = explode('</tr>' , $first_step[1] );

echo $second_step[0];

How to retrieve a particular cell ? (the second, for example...)

$url = 'folder/myPage.php';
$content = file_get_contents($url);

//Ugly Code !...doesn't work !

$first_step = explode('<tr id="foo"><td[1]' , $content );
$second_step = explode('</td></tr>' , $first_step[1] );

echo $second_step[0];

Thanks.Nicolas.


Solution

  • I have found the solution !! the second explode with the </td> and not with the </tr> :

    $url = 'folder/myPage.php';
    $content = file_get_contents($url);
    $first_step = explode('<tr id="' . $ref . '">' , $content );
    $second_step = explode('</td>' , $first_step[1] );
    
    echo $second_step[1]; // show the content of the second cell
    echo $second_step[5]; // show the content of the sixth cell
    // etc...
    
    // "$ref" is a loop with foreach in a list of elements
    

    I did not want to use a library for doing that (like Simple_Html_Dom : 1800 lines of code , just to watch a file !! too heavy.)

    With this solution, it's working like a charm ! I'm happy..:-) [solved]. Nicolas