473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

check string rep's integer

Is there a PHP function to verify that a string validly represents an
integer?
Jul 17 '05 #1
7 5468
*** sinister wrote/escribió (Fri, 21 Nov 2003 15:10:16 GMT):
Is there a PHP function to verify that a string validly represents an
integer?


Check is_int() manual page.

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #2
"sinister" <si******@nospam.invalid> writes:
Is there a PHP function to verify that a string validly represents an
integer?


See the Variable Functions chapter in the PHP manual:

http://www.php.net/manual/en/ref.variables.php

The answer may not be as simple as you'd initially think, so be
sure to read a function's description and user-contributed notes
before using it. Test whatever code you end up writing to make
sure it gives the expected results for both integers and non-integers.

You could also use a regular expression, but using variable functions
would probably make your intentions more clear.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 17 '05 #3

"Alvaro G Vicario" <al******************@telecomputeronline.com> wrote in
message news:1p*****************************@40tude.net...
*** sinister wrote/escribió (Fri, 21 Nov 2003 15:10:16 GMT):
Is there a PHP function to verify that a string validly represents an
integer?
Check is_int() manual page.


No; that checks whether a variable is an integer, not whether a string
variable corrected represents an integer.

I'm thinking of
"66" true
"00037" true
"afdfdfd" false
"-00218" true
"- 18" false

--
--
-- Álvaro G. Vicario - Burgos, Spain
--

Jul 17 '05 #4

"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f********@omega.dimensional.com...
"sinister" <si******@nospam.invalid> writes:
Is there a PHP function to verify that a string validly represents an
integer?
See the Variable Functions chapter in the PHP manual:

http://www.php.net/manual/en/ref.variables.php


Not sure I understand the connection to variable functions. I would have
thought that I need a function on a string, returning a boolean, and that
either (a) it's already in php, or (b) I'd have to write my own.

I've done (b) in C and C++.
The answer may not be as simple as you'd initially think, so be
sure to read a function's description and user-contributed notes
before using it. Test whatever code you end up writing to make
sure it gives the expected results for both integers and non-integers.

You could also use a regular expression, but using variable functions
would probably make your intentions more clear.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

Jul 17 '05 #5
"sinister" <si******@nospam.invalid> writes:
"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f********@omega.dimensional.com...
"sinister" <si******@nospam.invalid> writes:
Is there a PHP function to verify that a string validly represents an
integer?


See the Variable Functions chapter in the PHP manual:

http://www.php.net/manual/en/ref.variables.php


Not sure I understand the connection to variable functions. I would have
thought that I need a function on a string, returning a boolean, and that
either (a) it's already in php, or (b) I'd have to write my own.


Did you look at the list of functions at the above link? If none
of them seem suitable, could you explain why? As I mentioned in
my first followup, the solution isn't as obvious as it might seem
at first, but it's there if you read the documentation and user
comments.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 17 '05 #6

"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f**********@omega.dimensional.com...
"sinister" <si******@nospam.invalid> writes:
"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f********@omega.dimensional.com...
"sinister" <si******@nospam.invalid> writes:

> Is there a PHP function to verify that a string validly represents an > integer?

See the Variable Functions chapter in the PHP manual:

http://www.php.net/manual/en/ref.variables.php
Not sure I understand the connection to variable functions. I would have thought that I need a function on a string, returning a boolean, and that either (a) it's already in php, or (b) I'd have to write my own.


Did you look at the list of functions at the above link? If none
of them seem suitable, could you explain why? As I mentioned in


OK, looking at the page again, I see what you mean. Basically, it's (b)
"I'd have to write my own," but the user comments show how to make it short
and sweet.

Thanks.

Best,

S
my first followup, the solution isn't as obvious as it might seem
at first, but it's there if you read the documentation and user
comments.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

Jul 17 '05 #7
*** sinister wrote/escribió (Fri, 21 Nov 2003 20:14:29 GMT):
Check is_int() manual page.


No; that checks whether a variable is an integer, not whether a string
variable corrected represents an integer.


You are right. Probably intval() is a better choice.

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Csaba Gabor | last post by:
I wrote a .repeat(n) function for strings which seemed to work fine: String.prototype.repeat = function(n) { // repeats the string n times if (n<1) return ""; if (n<2) return this; for (var...
5
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h>...
1
by: redpayne | last post by:
Okay, I finally got this program to run according to what the book had us build it as. Now prof wants case 2 and case 3 to prompt again for input, check input to see if it is the correct type, then...
2
by: Thall | last post by:
Hey Gurus - I've seen a few solutions to this problem, but none of which I can do without a little help. Here's the situation The following code loops thru a sales report, using the sales rep ID...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
6
by: temper3243 | last post by:
Hi Can someone explain me what is happening below ? Why is it printing 401380 $ cat scanf.c #include<stdio.h> int main() { int i; scanf(" %d",&i);
6
by: =?Utf-8?B?VmVybm9uIFBlcHBlcnM=?= | last post by:
I am having trouble with the binding of a check box. I have a form for editing and adding reps. I am using a bindingsource and a bindingnavigator. I have on the form an "Active" checkbox, which...
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
5
by: herman | last post by:
How can I replace all occurrences of a character with another character in std string? For example, I want to replace '/' with '+' in my std::string I have looked at the replace() method in...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.