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

前端 未结 7 583
一向
一向 2021-01-11 20:38

I got a string like:

$str = \"CASH55.35inMyPocket\";

I want to get 55.35 only.

I tried:

$str = flo         


        
相关标签:
7条回答
  • 2021-01-11 21:41

    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
    
    0 讨论(0)
提交回复
热议问题