is_numeric does not work with decimal separator,

0

I'm using the is_numeric function in the case below does not work. For example, if $val = 0,2 wants to change to 0.2

Just to contextualize the other values could be:

  • a string = > In this case, I want you to be just between single quotes: 'example'.
  • empty = > If I have empty I want empty single quotation marks ''
  • Note: I've already tried: if (is_numeric($val){}

    foreach ($innerArray as $key=>$val) {
    
        if (is_numeric($val)){
            $val = str_replace(",", ".", $val);
            $aux .= $val . ",";
        } else {
            $aux .="'" . $val ."'" . ",";
        }
    }
    
        
    asked by anonymous 28.09.2015 / 13:36

    3 answers

    1

    Join number_format() with is_numeric - you can then use number_format with arguments to make it the format you want. example

    $number = '1,2';
    echo is_numeric(number_format($number));
    
    
    $number = ['1,2', 1, ''];
    foreach ($number as $val) {
        if (!empty($val) || !ctype_alnum($val)) echo "val: $val is_numeric ".is_numeric(number_format($val))."\n\r";
    }
    

    You can simply remove all the letters and leave only the numbers of doing the foreach:

    $number = 'abc1,2def';
    $number = preg_replace(/[a-zA-Z]/i, '', $number);
    echo 'is numeric? '.is_numeric(number_format($number));
    
        
    28.09.2015 / 14:46
    1

    Considerations

    Well, first I'll make some considerations regarding the validation logic implemented in your application, then focus on solving your problem. You'll decide which one is most convenient.

    Entry rules.

    Every application needs to have input rules. It is such a validation of user inputs, everything that you type is required to enforce validation.

    Considering that this value is a user input and your application will need to understand this entry as INT it will be necessary to validate, not to calculate the string entered.

    I strongly recommend you validate this input and do not transform the value to the correct output, so your application processing the value of the user input correctly.

    Performance

    Force the user to enter the value correctly, for example:

    float 0.2 instead of string 0,2 will improve performance of your application.

    Well, you will not have to impose a transformation of typecast convertion .

    Saving application performance cost.

    Good manners

    Security is paramount, so will we validate all user input before processing them?

    A simple if(is_numeric($string)) on user input can tell him whether the value he is typing is valid for his application to process. So you enforce validation rules in your application.

    Resolution

    If your application accepts this type of input and needs to convert it to INT or FLOAT you can do it in the following ways:

    First let's give replace string characters.

    $string = str_replace(',', '.', $string);
    

    Then just work with floatval ()

    $float_value = floatval( $string );
    

    This will convert the typecast string to int

    Looking like this:

    $string = '0,2';
    
    $string = str_replace(',', '.', $string);
    
    $float_value = floatval( $string );
    
    var_dump(floatval($float_value));  // 0.2
    
    var_dump(is_numeric($float_value)); // true
    

    In this way you can process entries such as 0,2 , 0.2 , 2 , -9999 and 2a0 which will be understood as 2

        
    28.09.2015 / 15:30
    1

    To change your string to a value, just do this, it will already work:

    <?php 
    function convertNumber($val)
    {
       return floatval(preg_replace('/([,])+/','.', $val));
    }  
    
    $val = '0,2';
    echo convertNumber($val);  
    
    ?>
    

    Here's how it works:

    link

      

    Note: Use the is_numeric() method to check if it is a number, not to convert it. The expected output is a boolean for the method and not a converted number.

    To apply to your loop, apply the method, but check if it contains a number in your string to apply the method:

    $values = array();
    
    foreach ($innerArray as $key=>$val) {
         if(is_numeric($val) || preg_match('/[0-9]+/',$val)) {
            $values[] = convertNumber($val);
         } else {
            $values[] = "'{$val}'";
         }
    }
    
    $saida = implode(', ', $values);
    
        
    28.09.2015 / 16:10