我正在尝试从 html 中获取“参考代码”,但当标签为 P 时只是“参考”
例如:031-0132-806-02
如何使用 preg_match 做到这一点???
如何使用 preg_match 获取此信息?
{
label: 'P',
available: false,
ref: '031-0132-806-02'
},
{
label: 'M',
available: false,
ref: '031-0132-806-03'
}]
}
},
我正在尝试从 html 中获取“参考代码”,但当标签为 P 时只是“参考”
例如:031-0132-806-02
如何使用 preg_match 做到这一点???
如何使用 preg_match 获取此信息?
{
label: 'P',
available: false,
ref: '031-0132-806-02'
},
{
label: 'M',
available: false,
ref: '031-0132-806-03'
}]
}
},
如果数字始终采用相同的模式,您可以这样做:
<?php
$string = "{
label: 'P',
available: false,
ref: '031-0132-806-02'
},
{
label: 'M',
available: false,
ref: '031-0132-806-03'
}]
}
},";
preg_match_all('![0-9]{3}\-[0-9]{4}\-[0-9]{3}\-[0-9]{2}!',$string,$matches);
print_r($matches);
?>
更新假设这是一个数据爬虫。
<?php
$url = 'http://www.urltocapture...';
function crawlSite($url){
$refIDs = array();
$string = file_get_contents($url);
preg_match_all('!\items: +?\[[^]]+\]!s',$string,$sets);
foreach($sets as $items){
foreach($items as $item){
$cleanupPattern = array('!\t+!','! +!','!(\r\n|\n|\r)+!','! +!');
$cleanupReplacements = array(' ',' ',""," ",);
$item = preg_replace($cleanupPattern,$cleanupReplacements,$item);
//echo $item."\n";
preg_match_all('!label: \'P\'[^\}]+([0-9]{3}\-[0-9]{4}\-[0-9]{3}\-[0-9]{2})[^\}]+}!',$item,$item_match);
if(!empty($item_match[1][0])){
$refIDs[] = $item_match[1][0];
}
}
}
return $refIDs;
}
$refIDs = crawlSite($url);
print_r($refIDs);
?>
使用 foreach 循环:
$string = <<<WUT
{
label: 'P',
available: false,
ref: '031-0132-806-02'
},
{
label: 'M',
available: false,
ref: '031-0132-806-03'
}]
}
},
WUT;
$ref = array();
preg_match_all('/(?P<labels>{\s*label:.*?})/s', $string, $m);
foreach($m['labels'] as $code){
if(strpos($code, "label: 'P'")){
preg_match('/ref: \'(.*?)\'/', $code, $n);
$ref[] = $n[1];
}
}
print_r($ref);