note 84793 modified in function.floatval by yannick

From: Date: Thu, 07 Aug 2008 13:54:17 +0000
Subject: note 84793 modified in function.floatval by yannick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-143272@lists.php.net to get a copy of this message
The last getFloat() function is not completely correct. 1.000.000 and 1,000,000 and its negative variants are not correctly parsed. For the sake of comparing and to make myself clear I use the name parseFloat in stead of getFloat for the new function: <?php function parseFloat($ptString) { if (strlen($ptString) == 0) { return false; } $pString = str_replace(" ", "", $ptString); if (substr_count($pString, ",") > 1) $pString = str_replace(",", "", $pString); if (substr_count($pString, ".") > 1) $pString = str_replace(".", "", $pString); $pregResult = array(); $commaset = strpos($pString,','); if ($commaset === false) {$commaset = -1;} $pointset = strpos($pString,'.'); if ($pointset === false) {$pointset = -1;} $pregResultA = array(); $pregResultB = array(); if ($pointset < $commaset) { preg_match('#(([-]?[0-9]+(\.[0-9])?)+(,[0-9]+)?)#', $pString, $pregResultA); } preg_match('#(([-]?[0-9]+(,[0-9])?)+(\.[0-9]+)?)#', $pString, $pregResultB); if ((isset($pregResultA[0]) && (!isset($pregResultB[0]) || strstr($preResultA[0],$pregResultB[0]) == 0 || !$pointset))) { $numberString = $pregResultA[0]; $numberString = str_replace('.','',$numberString); $numberString = str_replace(',','.',$numberString); } elseif (isset($pregResultB[0]) && (!isset($pregResultA[0]) || strstr($pregResultB[0],$preResultA[0]) == 0 || !$commaset)) { $numberString = $pregResultB[0]; $numberString = str_replace(',','',$numberString); } else { return false; } $result = (float)$numberString; return $result; } ?> Comparing of float parsing functions with the following function: <?php function testFloatParsing() { $floatvals = array( "22 000,76", "22.000,76", "22,000.76", "22 000", "22,000", "22.000", "22000.76", "22000,76", "1.022.000,76", "1,022,000.76", "1,000,000", "1.000.000", "1022000.76", "1022000,76", "1022000", "0.76", "0,76", "0.00", "0,00", "1.00", "1,00", "-22 000,76", "-22.000,76", "-22,000.76", "-22 000", "-22,000", "-22.000", "-22000.76", "-22000,76", "-1.022.000,76", "-1,022,000.76", "-1,000,000", "-1.000.000", "-1022000.76", "-1022000,76", "-1022000", "-0.76", "-0,76", "-0.00", "-0,00", "-1.00", "-1,00" ); echo "<table> <tr> <th>String</th> <th>floatval()</th> <th>getFloat()</th> <th>parseFloat()</th> </tr>"; foreach ($floatvals as $fval) { echo "<tr>"; echo "<td>" . (string) $fval . "</td>"; echo "<td>" . (float) floatval($fval) . "</td>"; echo "<td>" . (float) getFloat($fval) . "</td>"; echo "<td>" . (float) parseFloat($fval) . "</td>"; echo "</tr>"; } echo "</table>"; } ?> --was-- The last getFloat() function is not completely correct. 1.000.000 and 1,000,000 and its negative variants are not correctly parsed. For the sake of comparing and to make myself clear I use the name parseFloat in stead of getFloat for the new function: function parseFloat($ptString) { if (strlen($ptString) == 0) { return false; } $pString = str_replace(" ", "", $ptString); if (substr_count($pString, ",") > 1) $pString = str_replace(",", "", $pString); if (substr_count($pString, ".") > 1) $pString = str_replace(".", "", $pString); $pregResult = array(); $commaset = strpos($pString,','); if ($commaset === false) {$commaset = -1;} $pointset = strpos($pString,'.'); if ($pointset === false) {$pointset = -1;} $pregResultA = array(); $pregResultB = array(); if ($pointset < $commaset) { preg_match('#(([-]?[0-9]+(\.[0-9])?)+(,[0-9]+)?)#', $pString, $pregResultA); } preg_match('#(([-]?[0-9]+(,[0-9])?)+(\.[0-9]+)?)#', $pString, $pregResultB); if ((isset($pregResultA[0]) && (!isset($pregResultB[0]) || strstr($preResultA[0],$pregResultB[0]) == 0 || !$pointset))) { $numberString = $pregResultA[0]; $numberString = str_replace('.','',$numberString); $numberString = str_replace(',','.',$numberString); } elseif (isset($pregResultB[0]) && (!isset($pregResultA[0]) || strstr($pregResultB[0],$preResultA[0]) == 0 || !$commaset)) { $numberString = $pregResultB[0]; $numberString = str_replace(',','',$numberString); } else { return false; } $result = (float)$numberString; return $result; } Comparing of float parsing functions with the following function: function testFloatParsing() { $floatvals = array( "22 000,76", "22.000,76", "22,000.76", "22 000", "22,000", "22.000", "22000.76", "22000,76", "1.022.000,76", "1,022,000.76", "1,000,000", "1.000.000", "1022000.76", "1022000,76", "1022000", "0.76", "0,76", "0.00", "0,00", "1.00", "1,00", "-22 000,76", "-22.000,76", "-22,000.76", "-22 000", "-22,000", "-22.000", "-22000.76", "-22000,76", "-1.022.000,76", "-1,022,000.76", "-1,000,000", "-1.000.000", "-1022000.76", "-1022000,76", "-1022000", "-0.76", "-0,76", "-0.00", "-0,00", "-1.00", "-1,00" ); echo "<table> <tr> <th>String</th> <th>floatval()</th> <th>getFloat()</th> <th>parseFloat()</th> </tr>"; foreach ($floatvals as $fval) { echo "<tr>"; echo "<td>" . (string) $fval . "</td>"; echo "<td>" . (float) floatval($fval) . "</td>"; echo "<td>" . (float) getFloat($fval) . "</td>"; echo "<td>" . (float) parseFloat($fval) . "</td>"; echo "</tr>"; } echo "</table>"; } http://php.net/manual/en/function.floatval.php

« previous php.notes (#143272) next »