RE: [PHP] How to find if a string is an integer?
-Original Message- From: Alex Earl [mailto:[EMAIL PROTECTED] Sent: 12 June 2003 21:13 Jumping in a little late here, but what about is_numeric()? Haven't tried it, but the php manual for is_int says: Note: To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). The problem with is_numeric() is that something like 4e4 comes up as true, because it could be construed as a HEX value. Just a little nitpick here, but 4e4 is not hex but scientific notation representing 40,000. 0x4e4 would be a hex value representing, h, a little over 1200 at a rough guess. I'm pretty sure both of these have been suggested, but personally I'd go for either a pattern match or something like: if ((string)($value+0)===$value) Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
Why don't you write your own test function since it's getting so hard to find one. All you have to do is check if the string only has number characters. I would say this is a simple task! Good luck, Rafael On Thu, 12 Jun 2003 13:13:28 -0700 (MST) Alex Earl [EMAIL PROTECTED] wrote: Jumping in a little late here, but what about is_numeric()? Haven't tried it, but the php manual for is_int says: Note: To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). The problem with is_numeric() is that something like 4e4 comes up as true, because it could be construed as a HEX value. Alex -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php António Rafael C. Paiva Electronics and Telecommunications Dep. Aveiro University -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
Felipe Desiderati wrote: try this: if (ereg(^[0-9]+$, $_POST[var_int)) echo is int; That's not the best regexp. It doesn' take into account negative integers and isn't there a [[:numeric:]] or something like regexp that would work better. I'm looking for the simplest method and the one that takes the less processing power :) Calling up the regular expression engine dounds pretty processing intensive ... Sorry, I don´t know that you needed an effective snipet. And you are right, I only writed for positive integers. Try something like apply 2 type cast simunltaneous. if ( (string) ((int) $_POST[var_int]) == $_POST[var_int]) echo is int; And sorry for my english, []´s Felipe -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
snip Sorry, I don´t know that you needed an effective snipet. And you are right, I only writed for positive integers. Try something like apply 2 type cast simunltaneous. if ( (string) ((int) $_POST[var_int]) == $_POST[var_int]) echo is int; Why not just use the is_int() function? www.php.net/is_int Alex -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
snip Sorry, I don´t know that you needed an effective snipet. And you are right, I only writed for positive integers. Try something like apply 2 type cast simunltaneous. if ( (string) ((int) $_POST[var_int]) == $_POST[var_int]) echo is int; Why not just use the is_int() function? www.php.net/is_int Please try to use is_int() on a value passed from a form and see why. Also, in the beginning of this thread, the OP already said that is_int() had been tried. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
snip Sorry, I don´t know that you needed an effective snipet. And you are right, I only writed for positive integers. Try something like apply 2 type cast simunltaneous. if ( (string) ((int) $_POST[var_int]) == $_POST[var_int]) echo is int; Why not just use the is_int() function? www.php.net/is_int Please try to use is_int() on a value passed from a form and see why. Also, in the beginning of this thread, the OP already said that is_int() had been tried. ---John Holmes... I guess I missed the first part of the thread then. Thanks for the info. Alex -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
To be honest, I haven't really understood what three of them does... Is it the same value without type casting? Jean-Christian Imbeault [EMAIL PROTECTED] skrev i meddelandet news:[EMAIL PROTECTED] Dvdmandt wrote: It's not that killing... :p preg_match(#^-?[0-9]+$#,$_POST[var_int]); Not *that's* a mouthful ... I was also thinking about that === operator.. And this: if(((string)((int)$_POST[var_int]))==$_POST[var_int]) Cute. Might actually work too! Wonder if there would be any difference in using == or === in your suggestion? Jc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
On Friday 13 June 2003 03:49, DvDmanDT wrote: To be honest, I haven't really understood what three of them does... Is it the same value without type casting? It takes into account the variable type as well: (1 == '1') // true (1 === '1') // false -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* All is well that ends well. -- John Heywood */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
Jumping in a little late here, but what about is_numeric()? Haven't tried it, but the php manual for is_int says: Note: To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). On Thursday, June 12, 2003, at 03:31 PM, CPT John W. Holmes wrote: Please try to use is_int() on a value passed from a form and see why. Also, in the beginning of this thread, the OP already said that is_int() had been tried. -- Brent Baisley Systems Architect Landover Associates, Inc. Search Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
Jumping in a little late here, but what about is_numeric()? Haven't tried it, but the php manual for is_int says: Note: To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). The problem with is_numeric() is that something like 4e4 comes up as true, because it could be construed as a HEX value. Alex -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
Hmmm, didn't think about that. Here's something simple that seems to work in a couple of tests. if(strlen($testValue-0) == strlen($testValue)) { echo Is a number; } else { echo Is not a number; } Or, in one line: (strlen($testValue-0) == strlen($testValue) ? 1 : 0) Kind of a kludge, but it seems to work for positive, negative and decimals. On Thursday, June 12, 2003, at 04:13 PM, Alex Earl wrote: Jumping in a little late here, but what about is_numeric()? Haven't tried it, but the php manual for is_int says: Note: To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). The problem with is_numeric() is that something like 4e4 comes up as true, because it could be construed as a HEX value. Alex -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Brent Baisley Systems Architect Landover Associates, Inc. Search Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] How to find if a string is an integer?
I would like to test wether a $_POST var is an integer or not. What is the best way? I ahve tried is_int() but that fails b/c $_POST vars are always strings. is_numeric() doesn't help because it doesn't differentiate bewteen numbers (1.0) and integers (1). The best I have been able to come up with is: if (is_numeric($var) is_int((int)$var)) But I ma hoping there is something simpler ... Any ideas? thansk, Jean-Christian Imbeault -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
try this: if (ereg(^[0-9]+$, $_POST[var_int)) echo is int; []´s Felipe - Original Message - From: Jean-Christian IMbeault [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, June 11, 2003 9:44 PM Subject: [PHP] How to find if a string is an integer? I would like to test wether a $_POST var is an integer or not. What is the best way? I ahve tried is_int() but that fails b/c $_POST vars are always strings. is_numeric() doesn't help because it doesn't differentiate bewteen numbers (1.0) and integers (1). The best I have been able to come up with is: if (is_numeric($var) is_int((int)$var)) But I ma hoping there is something simpler ... Any ideas? thansk, Jean-Christian Imbeault -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
Felipe Desiderati wrote: try this: if (ereg(^[0-9]+$, $_POST[var_int)) echo is int; That's not the best regexp. It doesn' take into account negative integers and isn't there a [[:numeric:]] or something like regexp that would work better. I'm looking for the simplest method and the one that takes the less processing power :) Calling up the regular expression engine dounds pretty processing intensive ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
It's not that killing... :p preg_match(#^-?[0-9]+$#,$_POST[var_int]); I was also thinking about that === operator.. And this: if(((string)((int)$_POST[var_int]))==$_POST[var_int]) Jean-Christian Imbeault [EMAIL PROTECTED] skrev i meddelandet news:[EMAIL PROTECTED] Felipe Desiderati wrote: try this: if (ereg(^[0-9]+$, $_POST[var_int)) echo is int; That's not the best regexp. It doesn' take into account negative integers and isn't there a [[:numeric:]] or something like regexp that would work better. I'm looking for the simplest method and the one that takes the less processing power :) Calling up the regular expression engine dounds pretty processing intensive ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to find if a string is an integer?
Dvdmandt wrote: It's not that killing... :p preg_match(#^-?[0-9]+$#,$_POST[var_int]); Not *that's* a mouthful ... I was also thinking about that === operator.. And this: if(((string)((int)$_POST[var_int]))==$_POST[var_int]) Cute. Might actually work too! Wonder if there would be any difference in using == or === in your suggestion? Jc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php