Universal Grasp
Universal Grasp

Reputation: 1845

In Php, how to get float value from a mixed string?

I got a string like:

$str = "CASH55.35inMyPocket";

I want to get 55.35 only.

I tried:

$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:

Strings will most likely return 0 although this depends on the leftmost characters of the string.

So, flotval and (float) apparently only work if the string is something like: 55.35aAbBcCdDeEfF... but will NOT work if it is like: aAbBcC55.35dDeEfF

is there a way to get the float no matter the position of text?

Upvotes: 18

Views: 22665

Answers (7)

Biswadeep Sarkar
Biswadeep Sarkar

Reputation: 892

Modifying @hek2mgl answer to get both float 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

Upvotes: 0

Matt
Matt

Reputation: 1131

I think a preg_replace could be helpfull here (untested):

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

You could extend the preg a little more to only get [number].[number], but in this case I think it will work.

Upvotes: 4

hek2mgl
hek2mgl

Reputation: 158080

Use a regex:

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

Upvotes: 1

PeeHaa
PeeHaa

Reputation: 72681

What you have cannot be casted to a float, because it doesn't look like a float from PHP's perspective. It is possible to grab the value using regex though.

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

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

Which gets the numeric values from the following strings:

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

Basically it looks for numbers in the string. And optionally it also check for decimals after that.

Upvotes: 5

Danijel
Danijel

Reputation: 12709

If you dont 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) 

Upvotes: 46

Max
Max

Reputation: 1

function extract_numbers($string)
{
preg_match_all('/([\d]+)/', $string, $match);
return $match[0];
}

Upvotes: -1

ErickBest
ErickBest

Reputation: 4692

You can try a little Function to extract that value for you before using either 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 test:

echo myFloats("CASH55.35inMyPocket");

Upvotes: 2

Related Questions