943,948 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 426
  • PHP RSS
Aug 18th, 2009
0

Simple PHP Regex Question!

Expand Post »
Hi! I have a question that probably has a really simple answer . I have a regex that validates names:

PHP Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jeffc418 is offline Offline
64 posts
since Aug 2007
Aug 18th, 2009
0

Re: Simple PHP Regex Question!

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 @#$%@#
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Aug 18th, 2009
0

Re: Simple PHP Regex Question!

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:

PHP Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jeffc418 is offline Offline
64 posts
since Aug 2007
Aug 18th, 2009
0

Re: Simple PHP Regex Question!

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
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Aug 18th, 2009
0

Re: Simple PHP Regex Question!

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:

PHP Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jeffc418 is offline Offline
64 posts
since Aug 2007
Aug 18th, 2009
0

Re: Simple PHP Regex Question!

Well you could turn magic quotes off http://php.net/magicquotes or you can use the stripslashes function
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Aug 19th, 2009
0

Re: Simple PHP Regex Question!

yes you can use stripslashes function, its good one.
Reputation Points: 16
Solved Threads: 48
Posting Whiz
BzzBee is offline Offline
327 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to view a word file as html?
Next Thread in PHP Forum Timeline: Users have to login and logout twice, why?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC