Considerations
Well, first I will make some considerations before the validation logic implemented in your application, then I will focus on the resolution of your problem. You will decide which is more convenient.
Rules of entry.
Every application needs to have input rules. Such is the validation of user entries, everything it enters need to impose validation.
Taking into account that this value is a user input and your application may need to understand this entry as INT
it will be necessary to validate, not perform calculation on the typed string.
I strongly recommend you validate this entry in the source and not turn the value to the correct output, so your application processing the user input value correctly.
Performance
Force the user to correctly type the value, for example:
float 0.2
rather than string 0,2
will improve the performance of your application.
Because then you won’t need to impose a transformation of Typecast convertion.
Saving application performance cost.
Manners
Security is the overriding factor, so let’s validate all user entries before processing them?
A simple if(is_numeric($string))
in the user input you can tell him if the value he is typing is valid for your application to process. So you enforce validation rules in your application.
Resolution
If your application comes to accept this type of input and you need to convert it to INT
or FLOAT
you can do it in the following ways:
First let’s take a replace in the string characters.
$string = str_replace(',', '.', $string);
Then just work with the floatval()
$float_value = floatval( $string );
Thus will convert the typecast
string to int
Thus remaining:
$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
This way it will be possible to process entries such as: 0,2
, 0.2
, 2
, -9999
and 2a0
which will be understood as 2
0,2 is a string for having a comma.
is_numeric
won’t work.– Diego Souza
So the question would be: How can I take the values with comma. Only the ones that are numbers and turn them to Number.
– Bruno Nascimento
There are other comma values that are not treated as numbers?
– Marcelo de Andrade
There may be texts or anything else. In this application I’m developing I don’t know what kind of data comes in. Hence the problem. For example, if you arrive: [Hello, All right! ] => I just want it to be in simple quotes. If it comes empty [] =>I want to put simple quotes. If you arrive: [0,2] => I want to change to [0.2]
– Bruno Nascimento
In PHP numerical data use the American standard, where the decimal separator is the point. You need to change the input of
,
for.
before entering theis_numeric
– gmsantos
I guess you’ll have to do it using regex.
– user28595
I also think it will be around... I’m searching here how it would be with the preg_replace. If someone has a tip put there ;)
– Bruno Nascimento
This numeric string is bounded by comma only?
– rray
Where does this value come from ?
– Edilson
Bruno, please edit the question to add details: click on [Edit] just below the tags and update your post. Thanks!
– brasofilo
using regex ' d+, d+' does not address your problem? So, when the resulting array comes empty, it means that there are no numbers separated by comma, you just need to switch to the quotes.
– user28595
This numeric string is bounded by comma only?
– rray
@brunoNascimento added an answer. See if it will help you.
– juniorb2ss
is_numeric()
is not a conversion method, is used to check if it is a number, it returns the booleantrue
if it is a number, orfalse
if it’s not a number.– Ivan Ferrer
to convert to use number:
intval($sua_string_numerica)
for whole, andfloatval($sua_string_numerica)
to a float.– Ivan Ferrer