0

Using the simple_html_dom_parser, I am trying to extract the teamId number from the anchor tags href attribute, using regular expression to check if the table cell has an anchor tag.

$rowData= array();

foreach($table->find('tr') as $row ){

 $flight = array();

foreach ($row->find('td') as $cell){

if ($cell->find('a')){
  foreach ($cell as $anchor)
  $anchor = $cell->getAttribute('href');
  $pattern = '/^.*?teamId=(\d+).*$/';
  // write the pregmatch 
  preg_match_all($anchor, $pattern, $team_id);
  //put the team_id into the end flight array

  $flight[]= $team_id;
}

$flight[]= $cell->plaintext;
}
//pushes each TR into the array 


$rowData[] = $flight;
}

When I run the script, I get an empty regular epression error. I've used RegEx checkers to make sure I'm using the correct identifiers for getting the teamId from the href url. I cant find out if im incorrectly using the DOM parser for selecting the href value or if it is a logical error.

This is the value of the href in the anchor tag: /ffl/clubhouse?leagueId=347987&teamId=15&seasonId=2015

I want to put the matched teamId into the $flight array along with the other td (or $cells) from the table

  • Why are you using regex for this? Why don't you just access the string and split it? `explode('=',explode('&',explode('?',$urlstring)[1])[1])[1]` gives the result `15`from the example above. – junkfoodjunkie Nov 07 '16 at 21:47

1 Answers1

0

You should change this ...

if ($cell->find('a')){
  foreach ($cell as $anchor)

to this ...

foreach ($cell->find('a') as $anchor){

right now you are just converting the $cell to $anchor so you are looking for a href on a td element and not the a.

cmorrissey
  • 8,493
  • 2
  • 23
  • 27
  • Thank you for your help. I've been able to get the team_id stored into the $flight array, but it is also storing the href value as the first array item in $team_id. So when the $flight array is storing both the pattern match as well as the href – CbassOlympics Nov 07 '16 at 23:08