【发布时间】:2016-11-07 21:14:44
【问题描述】:
使用simple_html_dom_parser,我试图从锚标签href属性中提取teamId编号,使用正则表达式检查表格单元格是否有锚标签。
$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;
}
当我运行脚本时,我得到一个空的常规 epression 错误。我使用 RegEx 检查器来确保我使用正确的标识符从 href url 获取 teamId。我无法确定我是否错误地使用了 DOM 解析器来选择 href 值,或者它是否是一个逻辑错误。
这是锚标记中 href 的值: /ffl/clubhouse?leagueId=347987&teamId=15&seasonId=2015
我想将匹配的 teamId 与表中的其他 td(或 $cells)一起放入 $flight 数组中
【问题讨论】:
-
你为什么要使用正则表达式?为什么不直接访问字符串并将其拆分?
explode('=',explode('&',explode('?',$urlstring)[1])[1])[1]给出了上面示例的结果15。
标签: php regex-negation