Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

parseInt() in PHP (See related posts)

function parseInt($string) {
//	return intval($string);
	if(preg_match('/(\d+)/', $string, $array)) {
		return $array[1];
	} else {
		return 0;
	}
}


echo parseInt("2008"); // 2008
echo parseInt("99.90 dollars"); // 99
echo parseInt("www.w3.org"); // 3
echo parseInt("300 spartiates"); // 300
echo parseInt("block text..."); // 0


Source: AB-D Création de sites internet

Comments on this post

hach22 posts on May 15, 2008 at 05:41
function parseInt($string) {
// return intval($string);
if(preg_match('/(\d+)/', $string, $array)) {
return $array[1];
} else {
return false;//I think that this is better!!
}
}

You need to create an account or log in to post comments to this site.


Click here to browse all 4852 code snippets

Related Posts