17

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?

kupendra
  • 1,002
  • 1
  • 15
  • 37
Universal Grasp
  • 1,835
  • 3
  • 20
  • 29

7 Answers7

44

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) 
Danijel
  • 12,408
  • 5
  • 38
  • 54
5

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.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
4

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.

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Matt
  • 1,081
  • 15
  • 27
2

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");
ErickBest
  • 4,586
  • 5
  • 31
  • 43
  • 1
    This would also match `3.3.3` or `...` – hek2mgl Jun 21 '14 at 13:41
  • 1
    The answer is perfect for: `CASH55.35inMyPocket` – ErickBest Jun 21 '14 at 13:43
  • @ErickBest Answer like this - and especially your defensive comment - could cost you your 'dream job', as recruiters do sample Stack. You'd be probably branded something like: unimaginative coder, cannot anticipate real life scenarios, writes rigid code etc. ;) – Jeffz Jul 01 '20 at 14:39
1

Use a regex:

preg_match('/([0-9]+\.[0-9]+)/', 'CASH55.35inMyPocket', $matches);
$number = (float) $matches[1]; // 55.35
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0

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
Biswadeep Sarkar
  • 841
  • 9
  • 17
-1
function extract_numbers($string)
{
preg_match_all('/([\d]+)/', $string, $match);
return $match[0];
}
Max
  • 1
  • 1