|
-
Jul 2nd, 2006, 06:05 PM
#1
Thread Starter
Frenzied Member
Validating number
I need to validate a number.
nnn.nn.nn
Rules:
Three sections
Each section can contain only numbers
Each section can have any amount of numbers
Each section is seperated by a .
Now i have seen some type of validation, but im not sure how they string together?
Is there any assistance i can get?
ILMV
-
Jul 3rd, 2006, 12:41 AM
#2
Re: Validating number
You should use a regular expression:
PHP Code:
$regexp = "/([0-9]+)\.([0-9]+)\.([0-9]+)/";
if (preg_match($regexp,$string,$matches)) {
/* matches is now an array contiaing each component*/
print_r($matches);
}
-
Jul 3rd, 2006, 10:16 AM
#3
Re: Validating number
Can't you split a string entered by a character (I recall - haven't done any php for 3 wks .. might try tonight).
That creates an array - then you check that all the array elements are numeric.
If not - fail it ...
If so - continue on...
Different way
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Jul 3rd, 2006, 08:33 PM
#4
Re: Validating number
Probably. But remember PHP is a interpreted scripting language. The more you can make the PHP engine do with less code, the better. Efficiency is important on any website, and crucial on high-traffic sites.
-
Jul 4th, 2006, 02:27 AM
#5
Re: Validating number
There is a slight over head associated with regular expressions as PHP needs to compile them before executing. This makes the code about 30% slow than loading the string into an array and manually checking each component.
However, once a regular expression has been compiled by the script, it is then cached. When the cached regular expression is used, it is about 60% quicker.
Have a look below at the three tests:
http://adam.codedv.com/examples/speed/speed_test.php
If you run PHP as a module, the compiled regular expressions are shared among scripts. If you are running PHP as a CGI, each expression must be compiled once for each script execution.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|