简体   繁体   English

搜索以逗号作为数组中小数点的数字

[英]Searching for numbers with a comma as a decimal point in an array

I have to search through an Excel file and get the numbers that have a comma as a decimal point and convert it to a decimal point.(ex: 23,56 -> 23.56, 23123,566 -> 23123.566). 我必须搜索一个Excel文件并获取以逗号作为小数点的数字并将其转换为小数点。(例如:23,56-> 23.56、23123,566-> 23123.566)。 I have already exported the excel file to .csv and put all contents into arrays but I have trouble finding the numbers with comma. 我已经将excel文件导出到.csv并将所有内容放入数组中,但是我很难用逗号找到数字。 This is how one of my array looks like: 这是我的数组之一的样子:

    Array
    (
        [A1] => 1
        [A2] => 123123
        [A3] => dasdadwa
        [A4] => 6,7
        [A5] => 24f,5
        [A6] => f5,5
        [A7] => dasdad,fsdfsdfsfsasada dasdasd
        [A8] => aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
        [A9] => dasdasd
        [A10] => q231e
        [A11] => 
        [A12] => 0
        [A13] => 
        [A14] => 
        [A15] => 1
        [A16] => 123123
        [A17] => dasdadwa
        [A18] => 6,7
        [A19] => 24f,5
        [A20] => f5,5
        [A21] => dasdad,fsdfsdfsfsasada dasdasd
        [A22] => 
        [A23] => 
        [A24] => q231e
        [A25] => 
        [A26] => 0
        [A27] => 
        [A28] => 
        [A29] => 1
        [A30] => 123123
        [A31] => dasdadwa
        [A32] => 6,7
        [A33] => 24f,5
        [A34] => f5,5
        [A35] => dasdad,fsdfsdfsfsasada dasdasd
        [A36] => 
        [A37] => 
        [A38] => q231e
        [A39] => 
        [A40] => 
        [A41] => 
        [A42] => 
    )

I'm only interested in the characters that have only numbers and a comma but it is troublesome because those entries are strings. 我只对只包含数字和逗号的字符感兴趣,但是这很麻烦,因为这些条目是字符串。 I have tried messing with regex but I just could not get it to work. 我已经尝试过使用正则表达式,但是我无法使其正常工作。 Here is what i have so far: 这是我到目前为止所拥有的:

    function csv_to_array($filename='', $delimiter=',')
    {
        if(!file_exists($filename) || !is_readable($filename))
            return FALSE;

        $header = NULL;
        $data = array();
        if (($handle = fopen($filename, 'r')) !== FALSE)
        {
            while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
            {
                if(!$header)
                    $header = $row;
                else
                    $data[] = array_combine($header, $row);
            }
            fclose($handle);
        }
        return $data;
    }

$contents = csv_to_array($filename.'.csv');

    foreach($contents as $val) 
    {

        //If $val is a number separated by comma
             //Replace comma with . (str_replace(',','.');, $val);)
    }

Just simple pattern which will replace only your pattern: 只是简单的模式,它将仅替换您的模式:

//rest od your code and now:
foreach($contents as $key => $val) 
{
  $contents[$key] = preg_replace('/^(\d+),(\d+)$/', '\1.\2', $val);
}

This code will touch only strings like 2,3 . 此代码将仅接触2,3字符串。 Strings like this ff,333 will not be touch ff,333这样的字符串将不会被触摸

Since you would like to extract only decimals, I suggest using regex, like this: 由于您只想提取小数,因此建议使用正则表达式,如下所示:

foreach($contents as $val) {
    // if $val is a number separated by comma
    if (preg_match('/^[0-9]+,[0-9]+$/', $val) == 1) {
         // transform into float with two decimals and dot as decimal separator
         $float = number_format($val, 2, '.');
    }
}

Edit: updated regex pattern following comments. 编辑:在评论后更新了正则表达式模式。

Like this? 像这样?

foreach($contents as $val) 
{
    if (strpos($val, ',') !== false) {
        $val = str_replace(',', '.', $val);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM