I’m trying to scrape baseball information from a website – organize the info and then echo it as comma-delimited text with line breaks.
My code:
<?php
$html = file_get_contents(‘https://www.baseball-reference.com/leagues/MLB-standings.shtml’);
$tags = explode(‘<‘,$html);
foreach ($tags as $tag)
{
if (strpos($tag,’script’) !== FALSE) continue;
$text = strip_tags(‘<‘.$tag);
if (trim($text) != ”) $texts[] = ($text);
}
$HTML = json_encode($texts);
$startStr = ‘”last30″‘;
$endStr = ‘”Average”‘;
$startPos = strpos($HTML, $startStr) + strlen($startStr) + 1;
$endPos = strpos($HTML, $endStr ) – 1;
$Export = substr($HTML, $startPos, $endPos-$startPos);
echo $Export;
This displays:
“1”,”Philadelphia Phillies”,”28″,”12″,”.700″,”W 2″,”5.2″,”3.6″,”1.6″,”-0.7″,”0.9″,”26-14″,”2″,”5-3″,”7-7″,”10-0″,”6-2″,”15-7″,”13-5″,”2-1″,”8-2″,”19-6″,”9-6″,”6-3″,”22-9″,”9-1″,”16-4″,”23-7″,”2″,”Baltimore Orioles”,”26″,”12″,”.684″,”W 3″,”5.2″,”3.7″,”1.5″,”0.3″,”1.8″,”25-13″,”1″,”6-1″,”7-2″,”5-4″,”8-5″,”14-7″,”12-5″,”3-2″,”5-4″,”15-8″,”11-4″,”15-6″,”11-6″,”8-2″,”14-6″,”21-9″,”3″,”Los Angeles Dodgers”,”27″,”14″,”.659″,”W 1″,”5.4″,”3.5″,”1.9″,”-0.2″,”1.7″,”28-13″,”-1″,”11-4″,”4-3″,”8-5″,”4-2″,”15-8″,”12-6″,”1-3″,”5-5″,”15-10″,”12-4″,”13-9″,”14-5″,”8-2″,”15-5″,”19-11″,”4″,”Atlanta Braves”,”24″,”12″,”.667″,”W 4″,”5.0″,”3.7″,”1.3″,”-0.1″,”1.2″,”23-13″,”1″,”10-4″,”0-0″,”3-3″,”11-5″,”13-4″,”11-8″,”4-2″,”6-6″,”16-8″,”8-4″,”9-8″,”15-4″,”5-5″,”13-7″,”20-10″………..
What I’d like is for the results to display like this. A line break after each team’s stat line.
“1”,”Philadelphia Phillies”,”28″,”12″,”.700″,”W 2″,”5.2″,”3.6″,”1.6″,”-0.7″,”0.9″,”26-14″,”2″,”5-3″,”7-7″,”10-0″,”6-2″,”15-7″,”13-5″,”2-1″,”8-2″,”19-6″,”9-6″,”6-3″,”22-9″,”9-1″,”16-4″,”23-7″
“2”,”Baltimore Orioles”,”26″,”12″,”.684″,”W 3″,”5.2″,”3.7″,”1.5″,”0.3″,”1.8″,”25-13″,”1″,”6-1″,”7-2″,”5-4″,”8-5″,”14-7″,”12-5″,”3-2″,”5-4″,”15-8″,”11-4″,”15-6″,”11-6″,”8-2″,”14-6″,”21-9″
“3”,”Los Angeles Dodgers”,”27″,”14″,”.659″,”W 1″,”5.4″,”3.5″,”1.9″,”-0.2″,”1.7″,”28-13″,”-1″,”11-4″,”4-3″,”8-5″,”4-2″,”15-8″,”12-6″,”1-3″,”5-5″,”15-10″,”12-4″,”13-9″,”14-5″,”8-2″,”15-5″,”19-11″
“4”,”Atlanta Braves”,”24″,”12″,”.667″,”W 4″,”5.0″,”3.7″,”1.3″,”-0.1″,”1.2″,”23-13″,”1″,”10-4″,”0-0″,”3-3″,”11-5″,”13-4″,”11-8″,”4-2″,”6-6″,”16-8″,”8-4″,”9-8″,”15-4″,”5-5″,”13-7″,”20-10″………
How can I do this?