note 111582 added to function.intval

From: Date: Wed, 06 Mar 2013 09:58:28 +0000
Subject: note 111582 added to function.intval
Groups: php.notes 
Request: Send a blank email to php-notes+get-193954@lists.php.net to get a copy of this message
if you want to take a number from a string, no matter what it may contain, here is a good solution: <?php function int($s){return(int)preg_replace('/[^\-\d]*(\-?\d*).*/','$1',$s);} echo int('j18ugj9hu0gj5hg'); //output: 18 ?> this example returns an int, so it will follow the int rules, and has support for negative values. <?php function int($s){return($a=preg_replace('/[^\-\d]*(\-?\d*).*/','$1',$s))?$a:'0';} echo int('j-1809809808908099878758765ugj9hu0gj5hg'); //output: -1809809808908099878758765 ?> this one returns a string with just the numeric value. it also supports negative values. the latter is better when you have a 32 bit system and you want a huge int that is higher than PHP_MAX_INT. ---- Server IP: 69.147.83.199 Probable Submitter: 77.54.215.164 ---- Manual Page -- http://php.net/manual/en/function.intval.php Edit -- https://master.php.net/note/edit/111582 Del: integrated -- https://master.php.net/note/delete/111582/integrated Del: useless -- https://master.php.net/note/delete/111582/useless Del: bad code -- https://master.php.net/note/delete/111582/bad+code Del: spam -- https://master.php.net/note/delete/111582/spam Del: non-english -- https://master.php.net/note/delete/111582/non-english Del: in docs -- https://master.php.net/note/delete/111582/in+docs Del: other reasons-- https://master.php.net/note/delete/111582 Reject -- https://master.php.net/note/reject/111582 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#193954) next »