douchuitang0642 2016-02-12 16:12
浏览 31
已采纳

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

This is my string:

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

and i want to have an array which should look like this:

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",
);

so my tries look like this:

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

or like this

$array = str_split($string);

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

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

someone know how i can solve this? especially the "tricky part" (i put a comment in the code)

would be very nice if someone can answer :)

  • 写回答

2条回答 默认 最新

  • dongwen5019 2016-02-12 16:30
    关注

    After you mentioned in comments that you have raw data in CSV format, the use of regular expression is not the best solution.

    Instead split the data into an array with PHP's CSV functions, like 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);
    

    Output:

    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 €',
    )
    

    If you would like to get rid of the and % signs, and/or use the decimal point for numbers (to allow further calculations in PHP), then you could do this:

    $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);
    

    Which outputs:

    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',
    )
    

    Original answer based on regular expressions

    You could do this with a regular expression, but it depends on what your rules are for splitting other strings. This regular expression assumes the string will always have the same number of items in the same order, and concerning the F010 and F020 it assumes that these always each occupy exactly 4 characters and are always present:

    $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);
    

    This outputs:

    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',
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?