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

Sebastian Fagetan

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). I have already exported the excel file to .csv and put all contents into arrays but I have trouble finding the numbers with comma. 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);)
    }
nospor

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. Strings like this ff,333 will not be touch

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pasting decimal numbers in excel / comma and point decimal separator

From Dev

Decimal comma instead of decimal point

From Java

Pandas convert numbers with a comma instead of the point for the decimal separator from objects to numbers

From Dev

Forcing decimal point instead of comma

From Dev

In R print decimal comma instead of decimal point

From Dev

Variable using decimal comma instead of decimal point

From Dev

Regex - search for numbers, preceded by a comma or quote, that have no digits before the decimal point

From Dev

Searching comma-separated numbers within a cell

From Dev

Searching comma-separated numbers within a cell

From Dev

Make decimal point to whole numbers

From Dev

Make decimal point to whole numbers

From Dev

Show comma instead of point as decimal separator

From Dev

Show comma instead of point as decimal separator

From Dev

bc: decimal separator comma vs. point

From Dev

Creating comma delimited array of floating point numbers and naming the array after first element (string) of the line in a CSV file

From Dev

Convert floating point numbers to decimal numbers

From Dev

Allow dot and comma in numbers, not only for decimal

From Dev

Parsing numbers with a comma decimal separator in JavaScript

From Dev

Add comma separator to a numbers with 2 decimal points

From Dev

Google form regex for numbers with comma as decimal separator

From Dev

R decimal comma instead of decimal point in ggplot scales::percent

From Dev

Selecting floating point numbers in decimal form

From Dev

Conversion of decimal floating point numbers to binary and back

From Dev

In a unix timestamp should there be numbers after a decimal point?

From Dev

how to subtract numbers with decimal point in php

From Dev

Aligning numbers at decimal point angular ui grid

From Dev

Formatting Numbers So They Align On Decimal Point

From Dev

storing the numbers after the decimal point into a variable in MySQL

From Dev

Getting output of 2 numbers after the decimal point

Related Related

  1. 1

    Pasting decimal numbers in excel / comma and point decimal separator

  2. 2

    Decimal comma instead of decimal point

  3. 3

    Pandas convert numbers with a comma instead of the point for the decimal separator from objects to numbers

  4. 4

    Forcing decimal point instead of comma

  5. 5

    In R print decimal comma instead of decimal point

  6. 6

    Variable using decimal comma instead of decimal point

  7. 7

    Regex - search for numbers, preceded by a comma or quote, that have no digits before the decimal point

  8. 8

    Searching comma-separated numbers within a cell

  9. 9

    Searching comma-separated numbers within a cell

  10. 10

    Make decimal point to whole numbers

  11. 11

    Make decimal point to whole numbers

  12. 12

    Show comma instead of point as decimal separator

  13. 13

    Show comma instead of point as decimal separator

  14. 14

    bc: decimal separator comma vs. point

  15. 15

    Creating comma delimited array of floating point numbers and naming the array after first element (string) of the line in a CSV file

  16. 16

    Convert floating point numbers to decimal numbers

  17. 17

    Allow dot and comma in numbers, not only for decimal

  18. 18

    Parsing numbers with a comma decimal separator in JavaScript

  19. 19

    Add comma separator to a numbers with 2 decimal points

  20. 20

    Google form regex for numbers with comma as decimal separator

  21. 21

    R decimal comma instead of decimal point in ggplot scales::percent

  22. 22

    Selecting floating point numbers in decimal form

  23. 23

    Conversion of decimal floating point numbers to binary and back

  24. 24

    In a unix timestamp should there be numbers after a decimal point?

  25. 25

    how to subtract numbers with decimal point in php

  26. 26

    Aligning numbers at decimal point angular ui grid

  27. 27

    Formatting Numbers So They Align On Decimal Point

  28. 28

    storing the numbers after the decimal point into a variable in MySQL

  29. 29

    Getting output of 2 numbers after the decimal point

HotTag

Archive