In php, how to get float value from mixed string?

I got a line like:

$str = "CASH55.35inMyPocket"; 

I want to get only 55.35 .

I did my best:

 $str = floatval("CASH55.35inMyPocket"); if ($str > 0) { echo "Greater_Zero"; } else { echo "LessZERO!!"; } // echo "LessZERO!!"; 

I also tried:

 $str = (float)"CASH55.35inMyPocket"; if ($str > 0) { echo "Greater_Zero"; } else { echo "LessZERO!!"; } // echo "LessZERO!!"; 

According to the documentation:

Lines are likely to return 0 although this depends on the leftmost characters of the line.

So flotval and (float) obviously only work if the line looks like: 55.35aAbBcCdDeEfF ... but will NOT work if it looks like: aAbBcC55.35dDeEfF

Is there any way to get swimming regardless of the position of the text?

+8
string php
source share
7 answers

If you do not want to use regular expressions, use filter_var :

 $str = "CASH55.35inMyPocket"; var_dump( (float) filter_var( $str, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) ); // float(55.35) 
+21
source share

That which you cannot send to float, because it is not like float from the point of view of PHP. However, you can use the value with a regular expression.

If you are not sure whether there will always be a decimal. And you are trying to get the number regardless of the position in the text (according to your question). You can use:

 ^.*?([\d]+(?:\.[\d]+)?).*?$ 

Which gets numeric values ​​from the following lines:

 CASH55.35inMyPocket CASH55inMyPocket 55.35inMyPocket 55inMyPocket inMyPocket55.35 inMyPocket55 

Explanation: http://regex101.com/r/tM8eM0
Demo: http://rubular.com/r/Gw5HTzsejj
PHP demo: https://eval.in/165521

It basically searches for numbers in a string. And perhaps he also checks the decimal places after that.

+3
source share

Use regex:

 preg_match('/([0-9]+\.[0-9]+)/', 'CASH55.35inMyPocket', $matches); $number = (float) $matches[1]; // 55.35 
+1
source share

You can try a little function to extract this value for you before using floatval or (float) .

Something like:

 function myFloats($str) { if(preg_match("#([0-9\.]+)#", $str, $match)) { // search for number that may contain '.' return floatval($match[0]); } else { return floatval($str); // take some last chances with floatval } } 

then check:

 echo myFloats("CASH55.35inMyPocket"); 
+1
source share

I think preg_replace might be useful here (unchecked):

 $float = preg_replace('/[^0-9\.]/', "", "CASH55.35inMyPocket"); // only show the numbers and dots 

You can expand the preg a bit more to get [number]. [number], but in this case, I think it will work.

+1
source share

Modification of @ hek2mgl answer to get both floating point and integer numbers

 preg_match('/([0-9]+\.?[0-9].)/', 'CASH55.35inMyPocket', $matches); $number = (float) $matches[1]; // 55.35 preg_match('/([0-9]+\.?[0-9].)/', 'CASH55inMyPocket', $matches); $number = (float) $matches[1]; // 55 preg_match('/([0-9]+\.?[0-9].)/', 'Total.CASH55.35inMyPocket', $matches); //with a dot before number $number = (float) $matches[1]; // 55.35 
0
source share
 function extract_numbers($string) { preg_match_all('/([\d]+)/', $string, $match); return $match[0]; } 
-one
source share

All Articles