Connecting Tech Pros Worldwide Forums | Help | Site Map

php regular expressions

Newbie
 
Join Date: Nov 2006
Posts: 7
#1: May 13 '07
Hie

could someone please help me> I'm trying to use a regular expression to validate and address and it is as follows.

eregi('(^[a-zA-Z0-9 \\\.,-]+)$',$value4)

$value4 = 10/54 kent street;

I want to allow the users to put in slashes as part of their addresses so I'm looking 4 a simple regular expression to do that.

Thank you

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#2: May 13 '07

re: php regular expressions


Quote:

Originally Posted by simbarashe

Hie

could someone please help me> I'm trying to use a regular expression to validate and address and it is as follows.

eregi('(^[a-zA-Z0-9 \\\.,-]+)$',$value4)

$value4 = 10/54 kent street;

I want to allow the users to put in slashes as part of their addresses so I'm looking 4 a simple regular expression to do that.

Thank you

If I'm not very much mistaken, putting a '/' in front of your '\\\' would do the trick.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#3: May 15 '07

re: php regular expressions


You'll probably want something like this:

Expand|Select|Wrap|Line Numbers
  1. preg_match('/^[\d]+(\/[\d]+)?(\b\w)+$/', $value4);
  2.  
That would most closely match '10/54 kent street'.

The regular expression there matches at least one number, then optionally (a slash followed by at least one number), then at least one word.

If you want to be able to do something like '10/54 west 3rd street', you'll have to modify it slightly (assumes that street names only start with numbers and never have numbers in the middle or end):

Expand|Select|Wrap|Line Numbers
  1. preg_match('/^[\d]+(\/[\d]+)?(\b\d*\w)+$/', $value4);
  2.  
For more info:
http://www.regular-expressions.info/reference.html
Newbie
 
Join Date: Nov 2006
Posts: 7
#4: May 21 '07

re: php regular expressions


Quote:

Originally Posted by pbmods

You'll probably want something like this:

Expand|Select|Wrap|Line Numbers
  1. preg_match('/^[\d]+(\/[\d]+)?(\b\w)+$/', $value4);
  2.  
That would most closely match '10/54 kent street'.

The regular expression there matches at least one number, then optionally (a slash followed by at least one number), then at least one word.

If you want to be able to do something like '10/54 west 3rd street', you'll have to modify it slightly (assumes that street names only start with numbers and never have numbers in the middle or end):

Expand|Select|Wrap|Line Numbers
  1. preg_match('/^[\d]+(\/[\d]+)?(\b\d*\w)+$/', $value4);
  2.  
For more info:
http://www.regular-expressions.info/reference.html

I tried that script but it didnt do the trick, thanks for it though maybe I needed to twist and turn a little bit to make it suitable for my use. I managed to use the script that i had made before but however I have encountered and even bigger problem and it is as follows:

Could someone please help me with my regular expresssions because my script is behaving in a rather wierd way when I implement the regular expressions. I am checking names and addresses before I insert them into my database but the regular expression function is not working when the MYSQL INSERT QUERIES are running. Below is my script and I'm just going to briefly explain it. The isWord($value,$pageURL) funtion is in a file called dbFunction.php and its as follows:

Expand|Select|Wrap|Line Numbers
  1. function isWord($value,$pageURL){
  2. $value2 = trim(stripslashes($value));
  3. f((strlen($value2) == 0) or (!ereg("^[[:alpha:]'-]{1,100}$",$value2))){
  4. $uri = "//" . $_SERVER["SERVER_NAME"]. rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
  5. header("Location: http:".$uri.$pageURL."&err=1");
  6. }else{
  7. return $value2;
  8. }
  9. }
[Please use CODE tags when posting source code. Thanks! --pbmods]

I then called it in dbJobsheet as follows:

Expand|Select|Wrap|Line Numbers
  1. $firstName = isWord($_POST['firstName'],$errorPageURL);
this works perfectly because if I put in invalid characters I get redirected to the error page. However when I put in the script to insert the data into the database the above isWord($value,$pageURL) doesnt work.

the script that stops all this from working is as follows:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. $timeQuery = mysql_query("SELECT date_assinged,time_assigned,assStaffID 
  3.  
  4. FROM FOLLOW_UP WHERE
  5. time_assigned ='$newTime' AND 
  6. date_assinged = '$newAssignedDate' 
  7. AND assStaffID = '$assStaffID' ")
  8. or die(mysql_error());
  9.  
  10. $results = mysql_fetch_array($timeQuery);
  11. //checks if there is a time clash and redirects user to the page to re-enter the information. 
  12. if ((count($results)-1)>0){ 
  13. header('Location: index.php? 
  14. page=redirectOldClientsJobSheet.php&err=6'); } 
  15.  
  16. //checks is the contact exists
  17. elseif(mysql_num_rows(mysql_query("SELECT contactID FROM 
  18. CONTACTS WHERE first_name = '$firstName' 
  19. AND last_name = '$lastName' AND 
  20. email_address = '$email_address' "))){
  21.  
  22. mysql_query("INSERT INTO STATUS VALUES(
  23. NULL,'$status','$description') ") 
  24. or die (mysql_error());
  25.  
  26. $statusID = mysql_query("SELECT statusID FROM STATUS WHERE
  27. statusID = 'LAST_INSERT_ID()' ") or die (mysql_error());
  28.  
  29. mysql_query("INSERT INTO JOBSHEET VALUES(
  30. NULL,'$clientID',LAST_INSERT_ID(),'$staffID','$COD','$billed',
  31. CURRENT_DATE(),'$warranty','$jobID','$contactID','$title',
  32. '$complete','$log')") or die (mysql_error()); 
  33.  
  34. header('Location: index.php?page=oldClientsEnteredInfoDisplay.php'); 
  35. }
  36. else{ 
  37. header('Location: index.php?page=redirectOldClientsJobSheet.php&err=7');
  38. }
  39. ?>
Reply