Page 1 of 1

Get Digits

Posted: Sun Jun 12, 2005 9:28 pm
by MathewByrne
Hi,

I was wondering if anyone could help me with a function that strips all the digits from a string and returns them. Something like this:

Code: Select all

$input = "(04) 854 00912";
echo getDigit($input);
// Would print 0485400912

Posted: Sun Jun 12, 2005 9:35 pm
by hawleyjr

Code: Select all

/*
	REMOVE ANY NON 0-9 CHAR
	IF RETURN_INT IS TRUE
	RETURN THE INT VALUE
	SO 2A.3 WOULD RETURN 2
*/
function removeAllNonNumericChar($num,$return_int){
	$num = preg_replace('#[^\d\.]#','',$num);
	
	if($return_int && !ctype_digit($num) && strpos($num,'.') > 0){
		$num = substr($num."",0,strpos($num,'.'));
	}
	return $num;	
	
}

Posted: Sun Jun 12, 2005 10:32 pm
by MathewByrne
Thanks a heap

Posted: Mon Jun 13, 2005 7:11 am
by Chris Corbyn
Moved to regex.