如何使用 preg_match_all 检查 href 的值

How use preg_match_all to check href for a value

使用 simple_html_dom_parser,我试图从锚标记 href 属性中提取 teamId 编号,使用正则表达式检查 table 单元格是否具有锚标记。

$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;
}

当我 运行 脚本时,我得到一个空的正则表达式错误。我使用 RegEx 检查程序来确保我使用正确的标识符从 href url 获取 teamId。我无法确定我是否错误地使用 DOM 解析器来选择 href 值,或者它是否是一个逻辑错误。

这是锚标签中 href 的值: /ffl/clubhouse?leagueId=347987&teamId=15&seasonId=2015

我想将匹配的 teamId 与 table

中的其他 td(或 $cells)一起放入 $flight 数组

你应该改变这个...

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

到这个...

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

现在您只是将 $cell 转换为 $anchor,因此您正在寻找 td 元素上的 href 而不是 a .