preg_replace或preg_match以及如何正确定义它

时间:2016-02-12 16:12:56

标签: php regex preg-replace str-replace

这是我的字符串:

$string =  "format,surcharge,amount,1,card,basicprice 3,50  F010F020,0%,3,50 ,,,";

我希望有一个看起来像这样的数组:

array(
    0 => "format",
    1 => "surcharge",
    2 => "amount",
    3 => "1",
    4 => "card",
    5 => "basicprice",
    6 => "3,50",          //<-- tricky part, i complete don't get how i can solve this
    7 => "card",
    8 => "F010",          //<-- tricky part, i complete don't get how i can solve this   
    9 => "F020",          //<-- tricky part, i complete don't get how i can solve this
    10 => "3,50",
);

所以我的尝试看起来像这样:

$string =  "Format,Aufpreis,Anzahl,1,Card,Basispreis 3,50  F010F020,0%,3,50 ,,,,,,";
$regEx = '/,/';
$replace = ' ';
$perfectArray = preg_replace('/,/', $replace, $string2);
var_dump($perfectArray);

或者像这样

$array = str_split($string);

$from = array(",", '"');
$to = array(" ", " ");

    foreach ($array as $value)
    {
        $value = str_replace($from, $to, $string);
    }
    var_dump($value);

有人知道我怎么能解决这个问题?特别是“棘手的部分”(我在代码中发表评论)

如果有人可以回答,那将是非常好的。)

2 个答案:

答案 0 :(得分:2)

如您的评论所述,如果这是来自csv尝试使用

fgetcsv功能。

您可以直接处理这些值。

在这里看一下http://php.net/manual/en/function.fgetcsv.php,这个例子很好地解释了

答案 1 :(得分:1)

在评论中提到您有CSV格式的原始数据后,使用正则表达式不是最佳解决方案。

而是使用PHP的CSV函数将数据拆分为数组,例如str_getcsv

$csv = 'F251,43%,"3,50 €","0,50 €","0,50 €",,"0,50 €","0,50 €","0,50 €","0,49 €",
        "0,49 €",,"0,47 €",,"0,47 €",,"0,46 €","0,46 €","0,44 €","0,44 €","0,44 €",,
        "0,43 €",,"0,43 €","0,43 €",,,"0,41 €",,,"0,40 €","0,40 €",,"0,39 €",
        "0,39 €",,"0,37 €","0,37 €","0,36 €","0,36 €","0,36 €","0,36 €","0,36 €"';

$data = str_getcsv($csv);

var_export ($data);

输出:

array (
  0 => 'F251',
  1 => '43%',
  2 => '3,50 €',
  3 => '0,50 €',
  4 => '0,50 €',
  5 => '',
  6 => '0,50 €',
  7 => '0,50 €',
  8 => '0,50 €',
  9 => '0,49 €',
  10 => '0,49 €',
  11 => '',
  12 => '0,47 €',
  13 => '',
  14 => '0,47 €',
  15 => '',
  16 => '0,46 €',
  17 => '0,46 €',
  18 => '0,44 €',
  19 => '0,44 €',
  20 => '0,44 €',
  21 => '',
  22 => '0,43 €',
  23 => '',
  24 => '0,43 €',
  25 => '0,43 €',
  26 => '',
  27 => '',
  28 => '0,41 €',
  29 => '',
  30 => '',
  31 => '0,40 €',
  32 => '0,40 €',
  33 => '',
  34 => '0,39 €',
  35 => '0,39 €',
  36 => '',
  37 => '0,37 €',
  38 => '0,37 €',
  39 => '0,36 €',
  40 => '0,36 €',
  41 => '0,36 €',
  42 => '0,36 €',
  43 => '0,36 €',
)

如果您想摆脱%符号,和/或使用小数点(以允许在PHP中进一步计算),那么您可以这样做:< / p>

$data = array_map(function ($v) { 
    // get rid of `€` or `%` at the end of values:
    $v = preg_replace('/( €|%)$/', '', $v);
    // if you want to replace the decimal comma to point for further calculations:
    $num = str_replace(',', '.', str_replace('.', '', $v));
    return is_numeric($num) ? $num : $v;
}, $data);

var_export ($data);

哪个输出:

array (
  0 => 'F251',
  1 => '43',
  2 => '3.50',
  3 => '0.50',
  4 => '0.50',
  5 => '',
  6 => '0.50',
  7 => '0.50',
  8 => '0.50',
  9 => '0.49',
  10 => '0.49',
  11 => '',
  12 => '0.47',
  13 => '',
  14 => '0.47',
  15 => '',
  16 => '0.46',
  17 => '0.46',
  18 => '0.44',
  19 => '0.44',
  20 => '0.44',
  21 => '',
  22 => '0.43',
  23 => '',
  24 => '0.43',
  25 => '0.43',
  26 => '',
  27 => '',
  28 => '0.41',
  29 => '',
  30 => '',
  31 => '0.40',
  32 => '0.40',
  33 => '',
  34 => '0.39',
  35 => '0.39',
  36 => '',
  37 => '0.37',
  38 => '0.37',
  39 => '0.36',
  40 => '0.36',
  41 => '0.36',
  42 => '0.36',
  43 => '0.36',
)

基于正则表达式的原始答案

您可以使用正则表达式执行此操作,但这取决于您拆分其他字符串的规则。此正则表达式假定字符串将始终具有相同顺序的项目数,并且关于F010F020,它假定这些始终每个占用正好4个字符且始终存在:

$string = "format,surcharge,amount,1,card,basicprice 3,50  F010F020,0%,3,50 ,,,";

preg_match("/(.*?),(.*?),(.*?),(.*?),(.*?),(.*?)\s+(.*?)\s+(.{4})(.{4}),(.*?),(\d+,\d\d)\s/",
    $string, $matches);

var_export ($matches);

输出:

array (
  0 => 'format,surcharge,amount,1,card,basicprice 3,50  F010F020,0%,3,50 ',
  1 => 'format',
  2 => 'surcharge',
  3 => 'amount',
  4 => '1',
  5 => 'card',
  6 => 'basicprice',
  7 => '3,50',
  8 => 'F010',
  9 => 'F020',
  10 => '0%',
  11 => '3,50',
)