Jump to content

scraping from a page using file_get_contents and preg_match_all


Recommended Posts

Thanks in advance for help.  not sure what I'm doing wrong.  ive read the documents and from my understanding of the function, i expected it to work.  im trying to scrap from a page so i can make some changes systematically.  I need to separate out each field inside of the <td> tags </td>.  so the first thing i do is get a substr of the page so i know im getting the correct table.  i store it and then do the preg_match_all .  what i thought would work, inst parsing out the data fields inside the tags. 

 $html = file_get_contents($url);


$start_master = stripos($html, '<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="0">');    
$end_master = stripos($html, '</table>', $start_master+10);

// just get the entire table <table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="0">  to </table>
$htmlSection = substr($html, $start_master , $length);

  preg_match_all('@<td style=(.+)</td>@', $htmlSection, $matches2);
  $listItems2 = $matches2[1];

$j = 0;
   while ($j < count($listItems2))
  {
      echo "listItems2-1-".$j.": ".$listItems2[$j]."<br>"; 
    
      $j++;
  }

 

$htmlSection is:

<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="0"> 

 
<tr><td style="width: 275px;"><a href="/test1.html?ti=1327"><b><span style="color: #AB1717;">2023 NSA AUTUMN BY THE SEA - 1ST/2ND PLACE TEAMS WIN BOWNET PRIZE PACK </span></b></a></td><td style="width: 225px;">MAD BCH ROC/STARKEY RANCH</td><td style="width: 100px;">MAD BCH/TRINITY</td><td style="width: 15px;">2023-09-23</td><td style="width: 15px;">2023-09-24</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1326"><b><span style="color: #AB1717;">2023 NSA LIGHT OF AUBRIEN SCHOLARSHIP TOURNAMENT</span></b></a></td><td style="width: 225px;">BLACKSTONE PARK</td><td style="width: 100px;">PALMETTO</td><td style="width: 15px;">2023-09-23</td><td style="width: 15px;">2023-09-24</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1328"><b><span style="color: #AB1717;">2023 NSA LADY BOMBERS LESLEY JOHNSON 4TH ANNUAL MEMORIAL TOURNAMENT - 4 SEED INTO SINGLE ELIM</span></b></a></td><td style="width: 225px;">EC MOORE/NEW TAMPA COMM PARK</td><td style="width: 100px;">CLEARWATER/NEW TAMPA</td><td style="width: 15px;">2023-09-30</td><td style="width: 15px;">2023-10-01</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1329"><b><span style="color: #AB1717;">2023 NSA BATTLE FOR THE DOLLAR - WIN $$$ - EVERY TEAM WILL HAVE A CHANCE TO WIN CASH!</span></b></a></td><td style="width: 225px;">STARKEY RANCH</td><td style="width: 100px;">TRINITY</td><td style="width: 15px;">2023-10-07</td><td style="width: 15px;">2023-10-08</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1330"><b><span style="color: #AB1717;">2023 NSA/ST LEO REP THE ROAR TOURNAMENT - 1ST PLACE WINS BUSCH GARDENS SEASON PASSES!</span></b></a></td><td style="width: 225px;">EC MOORE/MAD BCH ROC</td><td style="width: 100px;">CLEARWATER/MAD BCH</td><td style="width: 15px;">2023-10-14</td><td style="width: 15px;">2023-10-15</td><td style="width: 50px;">8U - 14U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1331"><b><span style="color: #AB1717;">2023 NSA/ST LEO REVERSE EXPOSURE INVITATIONAL</span></b></a></td><td style="width: 225px;">EC MOORE</td><td style="width: 100px;">CLEARWATER</td><td style="width: 15px;">2023-10-14</td><td style="width: 15px;">2023-10-15</td><td style="width: 50px;">14U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1333"><b><span style="color: #AB1717;">2023 NSA KING OF THE HILL - 3 SEED INTO DOUBLE ELIM - 1ST PLACE WINS HOWL-O-SCREAM TICKETS!!</span></b></a></td><td style="width: 225px;">EC MOORE/MAD BCH ROC</td><td style="width: 100px;">CLEARWATER/MAD BCH</td><td style="width: 15px;">2023-10-21</td><td style="width: 15px;">2023-10-22</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1332"><b><span style="color: #AB1717;">2023 NSA PALMETTO SCARED HITLESS - 1ST PLACE WINS HOWL-O-SCREAM TICKETS!!</span></b></a></td><td style="width: 225px;">BLACKSTONE PARK</td><td style="width: 100px;">PALMETTO</td><td style="width: 15px;">2023-10-21</td><td style="width: 15px;">2023-10-22</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1334"><b><span style="color: #AB1717;">2023 NSA BOMBERWEEN - COSTUME CONTEST - PUMPKIN PRIZES!!</span></b></a></td><td style="width: 225px;">EC MOORE</td><td style="width: 100px;">CLEARWATER</td><td style="width: 15px;">2023-10-28</td><td style="width: 15px;">2023-10-29</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1335"><b><span style="color: #AB1717;">2023 NSA 13TH ANNUAL PITCH FOR THE CURE - OVER 80 TEAMS LAST YR - 4 SEED/SINGLE ELIM!</span></b></a></td><td style="width: 225px;">EC MOORE</td><td style="width: 100px;">CLEARWATER</td><td style="width: 15px;">2023-11-04</td><td style="width: 15px;">2023-11-05</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1337"><b><span style="color: #AB1717;">2023 NSA FALL BRAWL - 1ST PLACE WINS BUSCH GARDENS TICKETS!!</span></b></a></td><td style="width: 225px;">555 COMPLEX</td><td style="width: 100px;">BARTOW</td><td style="width: 15px;">2023-11-11</td><td style="width: 15px;">2023-11-12</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1336"><b><span style="color: #AB1717;">2023 NSA FALL BRAWL - 3 SEED INTO DOUBLE ELIMINATION!!</span></b></a></td><td style="width: 225px;">STARKEY RANCH</td><td style="width: 100px;">TRINITY</td><td style="width: 15px;">2023-11-11</td><td style="width: 15px;">2023-11-12</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1338"><b><span style="color: #AB1717;">2023 NSA BATTERBALL TURKEYFEST - DOUBLE/DOUBLE - 1ST & 2ND PLACE WIN NEW GLOVES!!</span></b></a></td><td style="width: 225px;">BLACKSTONE PARK</td><td style="width: 100px;">PALMETTO</td><td style="width: 15px;">2023-11-18</td><td style="width: 15px;">2023-11-19</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1339"><b><span style="color: #AB1717;">2023 NSA BATTERBALL TURKEYFEST - DOUBLE/DOUBLE - 1ST & 2ND PLACE WIN NEW GLOVES!!</span></b></a></td><td style="width: 225px;">MAD BCH ROC/STARKEY RANCH</td><td style="width: 100px;">MAD BCH/TRINITY</td><td style="width: 15px;">2023-11-18</td><td style="width: 15px;">2023-11-19</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1340"><b><span style="color: #AB1717;">2023 NSA THANKSGIVING THROWDOWN - 1 DAY/SUNDAY ONLY EVENT!!</span></b></a></td><td style="width: 225px;">NEW TAMPA COMM PARK</td><td style="width: 100px;">NEW TAMPA</td><td style="width: 15px;">2023-11-26</td><td style="width: 15px;">2023-11-26</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1341"><b><span style="color: #AB1717;">2023 NSA TOYS FOR TOTS #1 - 1ST & 2ND PLACE WIN NEW GLOVES!!</span></b></a></td><td style="width: 225px;">MADEIRA BEACH ROC</td><td style="width: 100px;">MADEIRA BEACH</td><td style="width: 15px;">2023-12-02</td><td style="width: 15px;">2023-12-03</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1342"><b><span style="color: #AB1717;">2023 NSA FALL STATE CHAMPIONSHIPS - REDEEM THE $$ YOU EARNED EACH TOURNAMENT THIS FALL!!</span></b></a></td><td style="width: 225px;">MULTIPLE VENUES</td><td style="width: 100px;">POLK COUNTY</td><td style="width: 15px;">2023-12-09</td><td style="width: 15px;">2023-12-10</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1343"><b><span style="color: #AB1717;">2023 NSA TOYS FOR TOTS #2 - CHANCE TO WIN SOME PRESENTS & COME MEET SANTA!!</span></b></a></td><td style="width: 225px;">NEW TAMPA COMM PARK</td><td style="width: 100px;">NEW TAMPA</td><td style="width: 15px;">2023-12-16</td><td style="width: 15px;">2023-12-17</td><td style="width: 50px;">8U - 18U</td></tr> 
</table>

 

 

the output is:

  • "width: 275px;">2023 NSA AUTUMN BY THE SEA - 1ST/2ND PLACE TEAMS WIN BOWNET PRIZE PACK MAD BCH ROC/STARKEY RANCHMAD BCH/TRINITY2023-09-232023-09-248U - 18U2023 NSA LIGHT OF AUBRIEN SCHOLARSHIP TOURNAMENTBLACKSTONE PARKPALMETTO2023-09-232023-09-248U - 18U2023 NSA LADY BOMBERS LESLEY JOHNSON 4TH ANNUAL MEMORIAL TOURNAMENT - 4 SEED INTO SINGLE ELIMEC MOORE/NEW TAMPA COMM PARKCLEARWATER/NEW TAMPA2023-09-302023-10-018U - 18U2023 NSA BATTLE FOR THE DOLLAR - WIN $$$ - EVERY TEAM WILL HAVE A CHANCE TO WIN CASH!STARKEY RANCHTRINITY2023-10-072023-10-088U - 18U2023 NSA/ST LEO REP THE ROAR TOURNAMENT - 1ST PLACE WINS BUSCH GARDENS SEASON PASSES!EC MOORE/MAD BCH ROCCLEARWATER/MAD BCH2023-10-142023-10-158U - 14U2023 NSA/ST LEO REVERSE EXPOSURE INVITATIONALEC MOORECLEARWATER2023-10-142023-10-1514U - 18U2023 NSA KING OF THE HILL - 3 SEED INTO DOUBLE ELIM - 1ST PLACE WINS HOWL-O-SCREAM TICKETS!!EC MOORE/MAD BCH ROCCLEARWATER/MAD BCH2023-10-212023-10-228U - 18U2023 NSA PALMETTO SCARED HITLESS - 1ST PLACE WINS HOWL-O-SCREAM TICKETS!!BLACKSTONE PARKPALMETTO2023-10-212023-10-228U - 18U2023 NSA BOMBERWEEN - COSTUME CONTEST - PUMPKIN PRIZES!!EC MOORECLEARWATER2023-10-282023-10-298U - 18U2023 NSA 13TH ANNUAL PITCH FOR THE CURE - OVER 80 TEAMS LAST YR - 4 SEED/SINGLE ELIM!EC MOORECLEARWATER2023-11-042023-11-058U - 18U2023 NSA FALL BRAWL - 1ST PLACE WINS BUSCH GARDENS TICKETS!!555 COMPLEXBARTOW2023-11-112023-11-128U - 18U2023 NSA FALL BRAWL - 3 SEED INTO DOUBLE ELIMINATION!!STARKEY RANCHTRINITY2023-11-112023-11-128U - 18U2023 NSA BATTERBALL TURKEYFEST - DOUBLE/DOUBLE - 1ST & 2ND PLACE WIN NEW GLOVES!!BLACKSTONE PARKPALMETTO2023-11-182023-11-198U - 18U2023 NSA BATTERBALL TURKEYFEST - DOUBLE/DOUBLE - 1ST & 2ND PLACE WIN NEW GLOVES!!MAD BCH ROC/STARKEY RANCHMAD BCH/TRINITY2023-11-182023-11-198U - 18U2023 NSA THANKSGIVING THROWDOWN - 1 DAY/SUNDAY ONLY EVENT!!NEW TAMPA COMM PARKNEW TAMPA2023-11-262023-11-268U - 18U2023 NSA TOYS FOR TOTS #1 - 1ST & 2ND PLACE WIN NEW GLOVES!!MADEIRA BEACH ROCMADEIRA BEACH2023-12-022023-12-038U - 18U2023 NSA FALL STATE CHAMPIONSHIPS - REDEEM THE $$ YOU EARNED EACH TOURNAMENT THIS FALL!!MULTIPLE VENUESPOLK COUNTY2023-12-092023-12-108U - 18U2023 NSA TOYS FOR TOTS #2 - CHANCE TO WIN SOME PRESENTS & COME MEET SANTA!!NEW TAMPA COMM PARKNEW TAMPA2023-12-162023-12-178U - 18U

 

what i expected was something like this:

2023 NSA AUTUMN BY THE SEA - 1ST/2ND PLACE TEAMS WIN BOWNET PRIZE PACK

MAD BCH ROC/STARKEY RANCH

MAD BCH/TRINITY

2023-09-23

2023-09-24

8U - 18U

[ ect to the next row ]

Try simple_html_dom

include('simple_html_dom.php');

$htmlSection = <<<HTML
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="0"> 
<tr><td style="width: 275px;"><a href="/test1.html?ti=1327"><b><span style="color: #AB1717;">2023 NSA AUTUMN BY THE SEA - 1ST/2ND PLACE TEAMS WIN BOWNET PRIZE PACK </span></b></a></td><td style="width: 225px;">MAD BCH ROC/STARKEY RANCH</td><td style="width: 100px;">MAD BCH/TRINITY</td><td style="width: 15px;">2023-09-23</td><td style="width: 15px;">2023-09-24</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1326"><b><span style="color: #AB1717;">2023 NSA LIGHT OF AUBRIEN SCHOLARSHIP TOURNAMENT</span></b></a></td><td style="width: 225px;">BLACKSTONE PARK</td><td style="width: 100px;">PALMETTO</td><td style="width: 15px;">2023-09-23</td><td style="width: 15px;">2023-09-24</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1328"><b><span style="color: #AB1717;">2023 NSA LADY BOMBERS LESLEY JOHNSON 4TH ANNUAL MEMORIAL TOURNAMENT - 4 SEED INTO SINGLE ELIM</span></b></a></td><td style="width: 225px;">EC MOORE/NEW TAMPA COMM PARK</td><td style="width: 100px;">CLEARWATER/NEW TAMPA</td><td style="width: 15px;">2023-09-30</td><td style="width: 15px;">2023-10-01</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1329"><b><span style="color: #AB1717;">2023 NSA BATTLE FOR THE DOLLAR - WIN $$$ - EVERY TEAM WILL HAVE A CHANCE TO WIN CASH!</span></b></a></td><td style="width: 225px;">STARKEY RANCH</td><td style="width: 100px;">TRINITY</td><td style="width: 15px;">2023-10-07</td><td style="width: 15px;">2023-10-08</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1330"><b><span style="color: #AB1717;">2023 NSA/ST LEO REP THE ROAR TOURNAMENT - 1ST PLACE WINS BUSCH GARDENS SEASON PASSES!</span></b></a></td><td style="width: 225px;">EC MOORE/MAD BCH ROC</td><td style="width: 100px;">CLEARWATER/MAD BCH</td><td style="width: 15px;">2023-10-14</td><td style="width: 15px;">2023-10-15</td><td style="width: 50px;">8U - 14U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1331"><b><span style="color: #AB1717;">2023 NSA/ST LEO REVERSE EXPOSURE INVITATIONAL</span></b></a></td><td style="width: 225px;">EC MOORE</td><td style="width: 100px;">CLEARWATER</td><td style="width: 15px;">2023-10-14</td><td style="width: 15px;">2023-10-15</td><td style="width: 50px;">14U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1333"><b><span style="color: #AB1717;">2023 NSA KING OF THE HILL - 3 SEED INTO DOUBLE ELIM - 1ST PLACE WINS HOWL-O-SCREAM TICKETS!!</span></b></a></td><td style="width: 225px;">EC MOORE/MAD BCH ROC</td><td style="width: 100px;">CLEARWATER/MAD BCH</td><td style="width: 15px;">2023-10-21</td><td style="width: 15px;">2023-10-22</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1332"><b><span style="color: #AB1717;">2023 NSA PALMETTO SCARED HITLESS - 1ST PLACE WINS HOWL-O-SCREAM TICKETS!!</span></b></a></td><td style="width: 225px;">BLACKSTONE PARK</td><td style="width: 100px;">PALMETTO</td><td style="width: 15px;">2023-10-21</td><td style="width: 15px;">2023-10-22</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1334"><b><span style="color: #AB1717;">2023 NSA BOMBERWEEN - COSTUME CONTEST - PUMPKIN PRIZES!!</span></b></a></td><td style="width: 225px;">EC MOORE</td><td style="width: 100px;">CLEARWATER</td><td style="width: 15px;">2023-10-28</td><td style="width: 15px;">2023-10-29</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1335"><b><span style="color: #AB1717;">2023 NSA 13TH ANNUAL PITCH FOR THE CURE - OVER 80 TEAMS LAST YR - 4 SEED/SINGLE ELIM!</span></b></a></td><td style="width: 225px;">EC MOORE</td><td style="width: 100px;">CLEARWATER</td><td style="width: 15px;">2023-11-04</td><td style="width: 15px;">2023-11-05</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1337"><b><span style="color: #AB1717;">2023 NSA FALL BRAWL - 1ST PLACE WINS BUSCH GARDENS TICKETS!!</span></b></a></td><td style="width: 225px;">555 COMPLEX</td><td style="width: 100px;">BARTOW</td><td style="width: 15px;">2023-11-11</td><td style="width: 15px;">2023-11-12</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1336"><b><span style="color: #AB1717;">2023 NSA FALL BRAWL - 3 SEED INTO DOUBLE ELIMINATION!!</span></b></a></td><td style="width: 225px;">STARKEY RANCH</td><td style="width: 100px;">TRINITY</td><td style="width: 15px;">2023-11-11</td><td style="width: 15px;">2023-11-12</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1338"><b><span style="color: #AB1717;">2023 NSA BATTERBALL TURKEYFEST - DOUBLE/DOUBLE - 1ST & 2ND PLACE WIN NEW GLOVES!!</span></b></a></td><td style="width: 225px;">BLACKSTONE PARK</td><td style="width: 100px;">PALMETTO</td><td style="width: 15px;">2023-11-18</td><td style="width: 15px;">2023-11-19</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1339"><b><span style="color: #AB1717;">2023 NSA BATTERBALL TURKEYFEST - DOUBLE/DOUBLE - 1ST & 2ND PLACE WIN NEW GLOVES!!</span></b></a></td><td style="width: 225px;">MAD BCH ROC/STARKEY RANCH</td><td style="width: 100px;">MAD BCH/TRINITY</td><td style="width: 15px;">2023-11-18</td><td style="width: 15px;">2023-11-19</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1340"><b><span style="color: #AB1717;">2023 NSA THANKSGIVING THROWDOWN - 1 DAY/SUNDAY ONLY EVENT!!</span></b></a></td><td style="width: 225px;">NEW TAMPA COMM PARK</td><td style="width: 100px;">NEW TAMPA</td><td style="width: 15px;">2023-11-26</td><td style="width: 15px;">2023-11-26</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1341"><b><span style="color: #AB1717;">2023 NSA TOYS FOR TOTS #1 - 1ST & 2ND PLACE WIN NEW GLOVES!!</span></b></a></td><td style="width: 225px;">MADEIRA BEACH ROC</td><td style="width: 100px;">MADEIRA BEACH</td><td style="width: 15px;">2023-12-02</td><td style="width: 15px;">2023-12-03</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1342"><b><span style="color: #AB1717;">2023 NSA FALL STATE CHAMPIONSHIPS - REDEEM THE $$ YOU EARNED EACH TOURNAMENT THIS FALL!!</span></b></a></td><td style="width: 225px;">MULTIPLE VENUES</td><td style="width: 100px;">POLK COUNTY</td><td style="width: 15px;">2023-12-09</td><td style="width: 15px;">2023-12-10</td><td style="width: 50px;">8U - 18U</td></tr><tr><td style="width: 275px;"><a href="/test1.html?ti=1343"><b><span style="color: #AB1717;">2023 NSA TOYS FOR TOTS #2 - CHANCE TO WIN SOME PRESENTS & COME MEET SANTA!!</span></b></a></td><td style="width: 225px;">NEW TAMPA COMM PARK</td><td style="width: 100px;">NEW TAMPA</td><td style="width: 15px;">2023-12-16</td><td style="width: 15px;">2023-12-17</td><td style="width: 50px;">8U - 18U</td></tr> 
</table>
HTML;

$dom = str_get_html($htmlSection);
foreach($dom->find("td") as $el)  {
    echo $el->innertext . '<br>';
}

giving

image.png.05b6a0a3a03d70cf2737ecaf52921cc4.png

etc.

37 minutes ago, wilsoc31 said:

is there another way?

Yes - DOMDocument, which you should have.

$dom = new DOMDocument;
$dom->loadHTML($htmlSection);
foreach ($dom->getElementsByTagname('td') as $el) {
    echo $el->nodeValue . '<br>';
}

 

41 minutes ago, wilsoc31 said:

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity, line: 4

You need to change all "&" in your html text to the correct html entities "&amp;" (simple_html_dom is more forgiving)

  1. ok, i can try that.  thank you so much for your help.  i noticed this way it removes the hyperlink under the first column.  is there an easy way to keep the hyperlink too?  im trying different solutions on google but not finding anything useful.

thanks again

Sure, simple_html_dom preserves the hyperlink (as my sample output showed)

The first bit of the html output from my original code was...

<a href="/test1.html?ti=1327"><b><span style="color: #AB1717;">2023 NSA AUTUMN BY THE SEA - 1ST/2ND PLACE TEAMS WIN BOWNET PRIZE PACK </span></b></a><br>
MAD BCH ROC/STARKEY RANCH<br>
MAD BCH/TRINITY<br>
2023-09-23<br>
2023-09-24<br>
8U - 18U<br>
<a href="/test1.html?ti=1326"><b><span style="color: #AB1717;">2023 NSA LIGHT OF AUBRIEN SCHOLARSHIP TOURNAMENT</span></b></a><br>
BLACKSTONE PARK<br>
PALMETTO<br>
2023-09-23<br>
2023-09-24<br>
8U - 18U<br>

Or you can try experimenting with DOMDocument  ( https://www.php.net/DOM )

It took me ages to find the right incantations for my second post.

  • 1 month later...

quick question here.. can you not use preg_match_all  to read a file.txt  and parse it out?  maybe i dont fully understand how preg_match_all works.  

 

i would have assumed that

reg_match_all("@<tr><td(.+)</td></tr>@", $html, $matches);

  $listItems = $matches[1];

 

would have parsed out the <tr><td> tags out of this file

<tr>
                                                    <td data-title='Tournament'><a href='https://some website.com'>Midwest Championship</a></td>
                                                    <td data-title='Location'>Rantoul, Illinois</td>
                                                    <td data-title='Organization'>Rock Tournaments</td>
                                                    <td data-title='GG'>4</td>
                                                    <td data-title='Start Date'>04-19-2024</td>
                                                    <td data-title='End Date'>04-21-2024</td>
                                                    <td data-title='Age Groups'>10U, 12U, 14U</td>
                                                </tr>
                                                                                            <tr>
                                                    <td data-title='Tournament'><a href='https://some website.com'>FAST PITCH SLUGFEST</a></td>
                                                    <td data-title='Location'>West Chicago, Illinois</td>
                                                    <td data-title='Organization'>Seminole Sports Softball</td>
                                                    <td data-title='GG'>4</td>
                                                    <td data-title='Start Date'>04-19-2024</td>
                                                    <td data-title='End Date'>04-21-2024</td>
                                                    <td data-title='Age Groups'>10U, 12U, 14U</td>
                                                </tr>

</tr>

 

but i get empty results.

 

thanks

11 minutes ago, wilsoc31 said:

can you not use preg_match_all  to read a file.txt  and parse it out?

I suppose , if you insist on using a hammer to drive in a screw. Not the right tool for the job.

so @Barand  i switched to use the simple dom  but for some reason im not getting the hperlink.  do you have an idea?  ive tried a few ways and researched but i keep getting the outer text "Links" but not the hyperlink

 

<li>
                                          <span class="label"><i class="fas fa-link"></i>Links</span>
                                          <span class="texts">
                                                                                        <a href="https://playpass.com/robinson-girls-youth-softball-association" target="_blank" class="social-link" title="Robinson Girls Youth Softball Association Website"><i class="fas fa-globe"></i></a>
                                                                                  </span>
                                      </li>

 

here is the code i tried:

foreach($dom2->find('<li>') as $el2)  
{
   echo "test weblink2: "; print_r($el2->href);  
}

 

but all i get is an empty line

Edited by wilsoc31
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.