[SOLVED] Regular Expression Question

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
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

[SOLVED] Regular Expression Question

Post by AGISB »

I am trying to validate a Query_String. It has to be exactly 5 digits. If it is correct then assign it to a session var otherwise set this session var to empty.

Here is what I was thinking:

Code: Select all

<?php

session_start();
if ( (ereg ("([0-9]{5})", $_SERVER['QUERY_STRING']) && (substr_count($_SERVER['QUERY_STRING'], "[0-9]") = 5) ) {
	$_SESSION['idnumber'] = $_SERVER['QUERY_STRING'] ;
}
else {
	$_SESSION['idnumber'] = '';
}

?>
Here is my question: Can I put the substr_count part right into the ereg function?
Last edited by AGISB on Wed Jul 21, 2004 1:51 pm, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

I'd avoid regex and just do:

Code: Select all

<?php
if(!empty($_SERVER['QUERY_STRING']) && ctype_digit($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) == 5){
  $_SESSION['idnumber'] = $_SERVER['QUERY_STRING'] ;
} else {
  $_SESSION['idnumber'] = '' ;
}
?>
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

Thanks. Besides that my version with subst_count didnt even work your version is better than my reworked version.

But if anyone can answer if the

strlen($_SERVER['QUERY_STRING']) == 5

can be added to the ereg funktion of my first post.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

Besides why do you use:

Code: Select all

!empty($_SERVER&#1111;'QUERY_STRING'])
Doesn't the 2nd and 3rd expression discard empty already?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You don't need the empty in this case as $_SERVER['QUERY_STRING'] is always set (even if it's empty), i just do it out of habit ;)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

I'm not sure why you even need the substr_count() in the regex as you're using {5} which says 'there must be 5 of them' so you'de just end up doing the same thing twice?
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

The substr_count aproach was my bad. strlen did the job.

Well ereg checks if 5 digits are in the string but also returns true if there are more. I am looking for the regular expression to check for exactly 5 ;)

This is more of a personal interest now as I have a working solution.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well ereg checks if 5 digits are in the string but also returns true if there are more. I am looking for the regular expression to check for exactly 5
ereg('^[0-9]{5}$' ....
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

markl999 wrote: ereg('^[0-9]{5}$' ....
In my understanding this would also be true if the string is: 01234s98765

5 digits at the end and 5 digits in the beginning.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Nope, ^ means beginning of string and $ is end of string, so ^[0-9]{5}$ means 5 digits from beginning to end.
Try it and see ;)

Code: Select all

<?php
$foo = '12345'; //valid
//$foo = '01234s98765'; //invalid

if(ereg('^[0-9]{5}$', $foo)){
  echo 'valid';
} else {
  echo 'invalid';
}
?>
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

Thanks. It shows once more not to trust every word written in a php book.
Post Reply