alpha-numeric fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

alpha-numeric fields

Post by aceconcepts »

Hi,

How can I check whether a field a certain contains letters and numbers? But in particular a certain amount of numbers.

i.e.

Field value = v41u3

How can I check if this field contains only 3 numbers?

Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

A regular expression should suffice.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

hi,

Thanks for the rapid response.

I've found that I can use this in a regex: '(?<!\S)\d++(?!\S)'

This will match whole numbers in a string.

Now how would I count the instances of the numbers?

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

\d matches a single digit.
preg_match_all searches for all matches in a subject.
Just count all matches of a single digit.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Could I be so bold as to ask for an illustration?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

preg_match_all() returns the number of matches. The pattern to use is only \d, nothing more. It can't get more simple then that.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Well, you have to use a delimiter either side of the regex (I use '~' and so the actual regex would be '~\d~')and use count() on the return value.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It wouldn't need count(). ;)
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Using preg_match_all how can I count how many numbers are in a field?
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

Are you counting _digits_ or _numbers_ in a string? If first, just test the string against

Code: Select all

$re = '~^[a-z]*([0-9][a-z]*){1,3}$~iD';
if it matches, the string is alphanumeric and contains 1, 2 or 3 digits (change {1,3} to {3} to require exactly 3).
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

What a wonderful piece of code! Works perfectly. Cheers.

Thanks a lot to all of u.
Post Reply