2013/3/16 Ashley Sheridan <ash@xxxxxxxxxxxxxxxxxxxx> > > > tamouse mailing lists <tamouse.lists@xxxxxxxxx> wrote: > > >On Fri, Mar 15, 2013 at 4:00 PM, Ashley Sheridan > ><ash@xxxxxxxxxxxxxxxxxxxx>wrote: > > > >> ** > >> On Fri, 2013-03-15 at 04:57 -0500, tamouse mailing lists wrote: > >> > >> On Fri, Mar 15, 2013 at 3:55 AM, Peter Ford <pete@xxxxxxxxxxxxx> > >wrote: > >> > On 15/03/13 06:21, Jim Lucas wrote: > >> >> > >> >> On 3/14/2013 4:05 PM, Matijn Woudt wrote: > >> >>> > >> >>> On Thu, Mar 14, 2013 at 11:44 PM, Jim Lucas <lists@xxxxxxxxx> > >wrote: > >> >>> > >> >>>> On 03/14/2013 11:50 AM, Samuel Lopes Grigolato wrote: > >> >>>> > >> >>>>> Something like "if (is_numeric($var)&& $var == floor($var))" > >will do > >> >>>>> the > >> >>>>> > >> >>>>> trick. I don't know if there's a better (more elegant) way. > >> >>>>> > >> >>>>> > >> >>>>> On Thu, Mar 14, 2013 at 3:09 PM, Matijn > >Woudt<tijnema@xxxxxxxxx> wrote: > >> >>>>> > >> >>>>> On Thu, Mar 14, 2013 at 7:02 PM, > >georg<georg.chambert@xxxxxxxxx**> > >> >>>>>> > >> >>>>>> wrote: > >> >>>>>> > >> >>>>>> Hi, > >> >>>>>>> > >> >>>>>>> > >> >>>>>>> I have tried to find a way to check if a character string is > >> >>>>>>> possible to > >> >>>>>>> test whether it is convertible to an intger ! > >> >>>>>>> > >> >>>>>>> any suggestion ? > >> >>>>>>> > >> >>>>>>> BR georg > >> >>>>>>> > >> >>>>>> > >> >>>>>> > >> >>>>>> You could use is_numeric for that, though it also accepts > >floats. > >> >>>>>> > >> >>>>>> - Matijn > >> >>>>>> > >> >>>>>> > >> >>>>> > >> >>>> for that type of test I have always used this: > >> >>>> > >> >>>> if ( $val == (int)$val ) { > >> >>>> > >> >>>> http://www.php.net/manual/en/**language.types.integer.php#** > >> >>>> > >> >>>> > >language.types.integer.casting< > http://www.php.net/manual/en/language.types.integer.php#language.types.integer.casting > > > >> >>>> > >> >>>> > >> >>>> > >> >>> I hope you're not serious about this... > >> >>> > >> >>> When comparing a string and an int, PHP will translate the string > >to int > >> >>> too, and of course they will always be equal then. > >> >>> So: > >> >>> $a = "abc"; > >> >>> if($a == (int)$a) echo "YES"; > >> >>> else echo "NO"; > >> >>> Will always return YES. > >> >>> > >> >>> - Matijn > >> >>> > >> >> > >> >> Hmmmm... Interesting. Looking back at my code base where I thought > >I was > >> >> doing that, turns out the final results were not that, but this: > >> >> > >> >> $value = "asdf1234"; > >> >> > >> >> if ( $value === (string)intval($value) ) { > >> >> > >> >> Looking back at the OP's request and after a little further > >searching, > >> >> it seems that there might be a better possible solution for what > >the OP > >> >> is requesting. > >> >> > >> >> <?php > >> >> > >> >> $values = array("asdf1234", "123.123", "123"); > >> >> > >> >> foreach ( $values AS $value ) { > >> >> > >> >> echo $value; > >> >> > >> >> if ( ctype_digit($value) ) { > >> >> echo ' - is all digits'; > >> >> } else { > >> >> echo ' - is NOT all digits'; > >> >> } > >> >> echo '<br />'.PHP_EOL; > >> >> } > >> >> > >> >> returns... > >> >> > >> >> asdf1234 - is NOT all digits > >> >> 123.123 - is NOT all digits > >> >> 123 - is all digits > >> >> > >> >> http://www.php.net/manual/en/function.ctype-digit.php > >> >> > >> >> An important note: > >> >> > >> >> This function expects a string to be useful, so for example > >passing in > >> >> an integer may not return the expected result. However, also note > >that > >> >> HTML forms will result in numeric strings and not integers. See > >also the > >> >> types section of the manual. > >> >> > >> >> -- > >> >> Jim > >> >> > >> > > >> > Integers can be negative too: I suspect your test would reject a > >leading > >> > '-'... > >> > >> > >> For my money, `is_numeric()` does just what I want. > >> > >> > >> > >> The thing is, is_numeric() will not check if a string is a valid int, > >but > >> any valid number, including a float. > >> > >> For something like this, wouldn't a regex be better? > >> > >> if(preg_match('/^\-?\d+$/', $string)) > >> echo int > >> > >> Thanks, > >> Ash > >> http://www.ashleysheridan.co.uk > >> > >> > >> > >That is so about is_numeric(), but once I know it's a numeric, coercing > >it > >to just an int is easy. *Validating*, rather than just assuring, that a > >string is an integer is another matter; if you need to give feedback to > >the > >user, etc., in which case a Regex is better. > > > >(One small nit, + is possible on integers, too.) > > The op wasn't about casting a string to an int but detecting if a string > was just a string representation of an int. Hence using a regex to > determine that. Regular expressions are not just about giving feedback to > the user. > Guess regex are the only useful solution here. When you consider to use built-in functions, just remember, that for example '0xAF' is an integer too, but '42.00' isn't. > > Thanks, > Ash > http://www.ashleysheridan.co.uk > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- github.com/KingCrunch