Simple PHP Regex Question!

Reply

Join Date: Aug 2007
Posts: 64
Reputation: jeffc418 is an unknown quantity at this point 
Solved Threads: 0
jeffc418 jeffc418 is offline Offline
Junior Poster in Training

Simple PHP Regex Question!

 
0
  #1
Aug 18th, 2009
Hi! I have a question that probably has a really simple answer . I have a regex that validates names:

  1. elseif (!preg_match('#^[a-z0-9\' -]{1,35}$#i',$lastname
  2. )) { include('includes/error.php'); }

Only if the name I enter has an apostrophe, it gives me my error message. Can someone please help me modify this regex so that it will allow a name like "O'Sullivan" or "Smith-Jacobs" to be entered? Thanks a ton!
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,410
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 226
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is online now Online
Code Monkey

Re: Simple PHP Regex Question!

 
0
  #2
Aug 18th, 2009
Strange, I tested it and it works fine for me. O'Reilly matches, Smith matches, Smith-Jacobs matches. The only thing that doesn't match is stuff like @#$%@#
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 64
Reputation: jeffc418 is an unknown quantity at this point 
Solved Threads: 0
jeffc418 jeffc418 is offline Offline
Junior Poster in Training

Re: Simple PHP Regex Question!

 
0
  #3
Aug 18th, 2009
Thanks for the reply. I've tried it on two browsers now and can't get "O'Sullivan" to validate, while "Smith-Jacobs" does just fine!

Here's what I'm using:

  1. <form action="" method="post">
  2. <input type="text" name="text">
  3. <input type="submit" name="submit">
  4. </form>
  5.  
  6. <?php
  7. if (isset($_POST['submit'])) {
  8. $text = $_POST['text'];
  9. if (!preg_match('#^[a-z0-9\' -]{1,35}$#i',$text
  10. )) { echo "fail"; }
  11. else { echo "success"; }
  12. }
  13. ?>

Please help . Much thanks!
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,410
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 226
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is online now Online
Code Monkey

Re: Simple PHP Regex Question!

 
0
  #4
Aug 18th, 2009
The place to start debugging is by echoing the variable to see if it is what you think it is. You may have magicquotes turned on and O'Reilly is actually O\'Reilly
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 64
Reputation: jeffc418 is an unknown quantity at this point 
Solved Threads: 0
jeffc418 jeffc418 is offline Offline
Junior Poster in Training

Re: Simple PHP Regex Question!

 
0
  #5
Aug 18th, 2009
Thank you! And if it's echoing with an escaped apostrophe, how can I go about stopping that for validation? I made a work-around:

  1. <form action="" method="post">
  2. <input type="text" name="text">
  3. <input type="submit" name="submit">
  4. </form>
  5.  
  6. <?php
  7. if (isset($_POST['submit'])) {
  8. $text = $_POST['text'];
  9. $text = str_replace("\\","",$text);
  10. if (!preg_match('#^[a-z0-9\' -]{1,35}$#i',$text
  11. )) { echo "fail"; }
  12. else { echo "success"; }
  13. }
  14. ?>

But that one line would be a significant modification to a large-portion of code. Is there an easy way to go about this? Thanks again!
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,410
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 226
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is online now Online
Code Monkey

Re: Simple PHP Regex Question!

 
0
  #6
Aug 18th, 2009
Well you could turn magic quotes off http://php.net/magicquotes or you can use the stripslashes function
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 257
Reputation: BzzBee is an unknown quantity at this point 
Solved Threads: 37
BzzBee BzzBee is offline Offline
Posting Whiz in Training

Re: Simple PHP Regex Question!

 
0
  #7
Aug 19th, 2009
yes you can use stripslashes function, its good one.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC