dongshou6041 2016-11-07 21:14
浏览 86
已采纳

如何使用preg_match_all检查href的值

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

  • 写回答

1条回答 默认 最新

  • duangai1368 2016-11-07 21:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch