php的正则表达式问题


regex issue with php

对于一个PHP项目,我需要从一个没有API的二手车网站获取数据。为了获取数据,我在PHP中使用file_get_contents()来获取所有HTML和regex来查找我要查找的数据。

这是我正在使用的数据:

<!-- begin Site parameters -->
sSite="autoscout24.nl";
sZone="used_car_detail";
adParams ={"make": "9","model": "1624","price": "2","fr": "7","miles": "10","art": "1","ad": "dealer","zip": "NL4264 AT","zip2": "4264 AT","did": "12865153","seal": "146","seg": "de_oem,mass_oem,compact,old_fr,high_miles,low_price,high_hp","hp": "7","acc": "U","vat": "0","fuel": "B","gear": "M","carno": "74RTBJ","carby": "0","ECO": "NO","equi": "1,2,3,5,12,13,17,30,31,32,38,49,126,127","type": "U","cost": "7450","img": "http://pic2.autoscout24.net/images/010/472/0257472010001.jpg","stmak": "Audi","stmod": "A3","sthp": "150","stkw": "110","age": "108","styea": "2005","stmon": "8","stmil": "232375","stccm": "1984","eutax": "0","ken": "74RTBJ","kenteken": "true","carid": "257472010","width": "3","test": "off","rnd": "72"};
<!-- end Site parameters -->

现在,我想选择的位是'74RTBJ'(不带引号)。我正在尝试使用

/"ken": "(.*?)",/sig

这样做,但是选择

 /"ken": "74RTBJ",

我如何让它只选择我想要的位?同样,我使用PHP的preg_match_all()来查找数据,我听说这很重要。

谢谢!

preg_match_all有第三个参数,它是对匹配组数组的引用。在您的示例中,在索引1下捕获引号之间的值。

preg_match_all('/"ken": "(.*?)",/sig', <your-string-here>, $matches);

现在:

$matches[0] == '"ken": "74RTBJ",'

,

$matches[1] == '74RTBJ'

更多信息:http://php.net//manual/pl/function.preg-match-all.php

只需在正则表达式中使用lookbehind或'K来匹配字段Ken的值,

'bken":'s*"'K[^"]*

(?<='bken": ")[^"]*
演示