note 109790 modified in function.is-float by danbrown

From: Date: Fri, 31 Aug 2012 15:48:57 +0000
Subject: note 109790 modified in function.is-float by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190337@lists.php.net to get a copy of this message
Yet another regular expression for float in real life:

<?php
function isTrueFloat($val)
{
	$pattern = '/^[-+]?(((\\\\d+)\\\\.?(\\\\d+)?)|\\\\.\\\\d+)([eE]?[+-]?\\\\d+)?$/';

	return (!is_bool($val) && (is_float($val) || preg_match($pattern, trim($val))));
}
?>

// Matches:
1, -1, 1.0, -1.0, '1', '-1', '1.0', '-1.0', '2.1', '0', 0, ' 0 ', ' 0.1 ', ' -0.0 ', -0.0, 3., '-3.', '.27', .27, '-0', '+4', '1e2', '+1353.0316547', '13213.032468e-13465', '-8E+3', '-1354.98879e+37436'

// Non-matches:
false, true, '', '-', '.a', '-1.a', '.a', '.', '-.', '1+', '1.3+', 'a1', 'e.e', '-e-4', 'e2', '8e', '3,25' --was-- Yet another regular expression for float in real life: function isTrueFloat($val) { $pattern = '/^[-+]?(((\\d+)\\.?(\\d+)?)|\\.\\d+)([eE]?[+-]?\\d+)?$/'; return (!is_bool($val) && (is_float($val) || preg_match($pattern, trim($val)))); } // Matches: 1, -1, 1.0, -1.0, '1', '-1', '1.0', '-1.0', '2.1', '0', 0, ' 0 ', ' 0.1 ', ' -0.0 ', -0.0, 3., '-3.', '.27', .27, '-0', '+4', '1e2', '+1353.0316547', '13213.032468e-13465', '-8E+3', '-1354.98879e+37436' // Non-matches: false, true, '', '-', '.a', '-1.a', '.a', '.', '-.', '1+', '1.3+', 'a1', 'e.e', '-e-4', 'e2', '8e', '3,25' http://php.net/manual/en/function.is-float.php

« previous php.notes (#190337) next »