我正在尝试从以下字符串/文本中提取金额:
Offerlist: 19939 Product: Technic Time: 13.01.16 - 14:08 Delivery: txt Offer36: 0,00 EUR Offer38: 185€ Best-Offer: 0,00 Offer5: 100 Offer1: 000000 Offer34: 80,00€ Offer5443: 185€ Offer876a: 00 Best-Offer: 200
我试过这个:
if (preg_match("/(?<=Offer1:)(.*?)(?=Offer34:)/s", $output, $result)) {
$offer = trim($result[0]);
}
但问题是,如果名称或位置发生变化,脚本将不再起作用。
答案 0 :(得分:1)
也许你应该搜索价值而不是它的兄弟姐妹。
因此以下脚本应该更好用:
$dates = [];
if (preg_match_all("/(\w+\d*): (\d+(,\d+)?[€]?|\d\d\.\d\d.\d\d \- \d\d:\d\d|\w+)/s", $output, $result, PREG_SET_ORDER)) {
foreach ($result as $data) {
$dates[] = [$data[1] => $data[2]];
}
}
答案 1 :(得分:1)
使用preg_match_all()和非常明确的编程可以这样做:
$string = 'Offer1: 000000 Offer34: 80,00€ Offer5443: 185€ Offer876a: 00 Best-Offer: 200';
$regex = '/((best-)?offer[^:]*:)([ 0-9,€]+)/i';
preg_match_all($regex, $string, $matches);
$offers = [];
for($c = 0; $c < count($matches[1]); $c++) {
$label = substr($matches[1][$c],0,-1);
$offers[$label] = trim($matches[3][$c]);
}
var_dump($offers);
输出:
array(5) {
["Offer1"]=>
string(6) "000000"
["Offer34"]=>
string(8) "80,00€"
["Offer5443"]=>
string(6) "185€"
["Offer876a"]=>
string(2) "00"
["Best-Offer"]=>
string(3) "200"
}
可以在http://sandbox.onlinephpfunctions.com/code/549db3d4d797e5821ede99612b18ac24d19ce9e8
找到实例