-1

I am trying to get certain parts of a page to be shown in the output.

<?php //right wing - up to ages 22 - potential range of 84 to 99 
$ch = curl_init("https://sofifa.com/players? 
aeh=22&ptl=84&pth=99&pn=27&pn=25&pn=23");// This will do
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$output = curl_exec($ch);
echo $output;
curl_close($ch); 
?> 

The code above prints the whole html of the URL. I want certain parts to show, such as the players name. Image below shows the whole page being outputted. I am trying to only get the players name to be shown, as well as their rating, age and potential.. So for example the page should output : L SANE : AGE: 21 OV : 84 : PO 92.

Is there any way i can do this? Image of page

1 Answers1

0

you don't use curl to display parts of the page, you use it to fetch the page. then, once you got the page, you must use something else to parse out the part of the page that you're interested in. the page is written in HTML, check this answer for a list of HTML parsing utilities for PHP.

as for how to parse out the specific info from this specific page, there's only 1 tbody tag in the page, and each player has their own dedicated tr tag, that is a direct child of that tbody tag, so you can iterate the tr children of the tbody tag to iterate through the players. each of these tr tags has td children containing the info you want. the text content of the 2nd td tag has their name, the 3rd has the age, the 4th has the rating, and the 5th has the potential.

an example using the DOMDocument parser:

<?php
declare(strict_types = 1);
$ch = curl_init ( "https://sofifa.com/players?aeh=22&ptl=84&pth=99&pn=27&pn=25&pn=23" );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
$output = curl_exec ( $ch );
curl_close ( $ch );
$domd = @DOMDocument::loadHTML ( $output );
foreach ( $domd->getElementsByTagName ( "tbody" )->item ( 0 )->getElementsByTagName ( "tr" ) as $tr ) {
    $tds = $tr->getElementsByTagName ( "td" );
    $player = array (
            'name' => trim ( $tds->item ( 1 )->textContent ),
            'age' => trim ( $tds->item ( 2 )->textContent ),
            'rating' => trim ( $tds->item ( 3 )->textContent ),
            'potential' => trim ( $tds->item ( 4 )->textContent ) 
    );
    print_r ( $player );
}
hanshenrik
  • 19,904
  • 4
  • 43
  • 89