我在一个看起来像这样的文件中有字符串(昏迷或点用作小数点):
$str = "12,50 EUR, 3,5%";
$str = "12,50EUR, 3,5%";
$str = "12,50EUR,3,5%";
$str = "2,5%";
$str = "8,75 EUR";
$str = "12.50 EUR, 3,5%";
$str = "12,50 EUR, 3.5%";
$str = "2.5%"
$str = "8.75 EUR";
$str = "8.75EUR";
$str = "8,75EUR";
我希望能够从每个字符串中提取数值,然后在数字后面的任何符号中删除任何空格或字符之间。
作为
的结果关联数组的示例$str = "12,50 EUR, 3,5%";
应该是这样的(结果值始终使用点作为小数分隔符,无论之前是什么):
array = {
[12.75] => 'EUR',
[3.5] => '%'
}
答案 0 :(得分:0)
<?php
$str []= "12,50 EUR, 3,5%";
$str []= "1EUR,2%";
$str []= "12,50EUR, 3,5%";
$str []= "12,50EUR,3,5%";
$str []= "2,5%";
$str []= "8,75 EUR";
$str []= "12.50 EUR, 3,5%";
$str []= "12,50 EUR, 3.5%";
$str []= "2.5%";
$str []= "8.75 EUR";
$str []= "8.75EUR";
$str []= "8,75EUR";
foreach ($str as $string) {
preg_match_all("/([0-9]+,*\.*[0-9]*\s*[A-Za-z%]*)/", $string, $results);
$ret= array();
if (isset($results[0][0])) {
preg_match("/([0-9]+,*\.*[0-9]*)(\s*[A-Za-z%]*)/", $results[0][0], $res);
$num = str_replace(",", ".", $res[1]);
$sym = trim($res[2]);
$ret[$num] = $sym;
}
if (isset($results[0][1])) {
preg_match("/([0-9]+,*\.*[0-9]*)(\s*[A-Za-z%]*)/", $results[0][1], $res);
$num = str_replace(",", ".", $res[1]);
$sym = trim($res[2]);
$ret[$num] = $sym;
}
var_dump($ret);
}