LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2008, 01:21 PM   #1
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Arrow Looking for feedback on my PHP filter_var regular expression


I'm a PHP newbie, and even worse when it comes to regular expressions. So basically I'm looking for any feedback about whether I'm doing this right or not. I'm performing a check on some XHTML form input (a person's first name). I decided I only want to allow letters, dashes, and individual spaces. Here's what I've currently got:
PHP Code:
if (filter_var($NameFILTER_VALIDATE_REGEXP,
array(
"options"=>array("regexp"=>"/[^A-Za-z\-][ ]{1}/"))))
   {
      
// Bad input detected.
   
}
else
   {
      
// Input looks good.
   

The [^A-Za-z\-][ ]{1} regular expression I put together seems to do what I want (at least in my rudimentary tests), but does it look okay to you? I will add more thorough checking later, right now I just wanna make sure I'm doing this basic checking properly. TIA.

Last edited by win32sux; 09-03-2008 at 01:24 PM.
 
Old 09-03-2008, 02:24 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
The reg exp looks ok, but the use of filter_var() with a reg exp seems a bit counter productive imho as you are not using the built in filter for filter_var but your own reg exp instead..
Why not use preg_match() ?
PHP Code:
if(preg_match("/[^A-Za-z\-][ ]{1}/"$Name)) {
    
// Bad input detected.
} else {
    
// Input looks good. 



Last edited by keefaz; 09-03-2008 at 02:37 PM. Reason: got confused with the ^ !
 
Old 09-03-2008, 03:32 PM   #3
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Thanks for the advice, I shall follow it and use preg_match() instead.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP PRCE Regular Expression problem x_terminat_or_3 Programming 4 09-11-2007 04:09 PM
Regular Expression msvinaykumar Programming 2 08-14-2006 08:48 AM
Simple Regular Expression in PHP tooparam Programming 7 03-09-2006 11:22 PM
php regular expression for mm-dd-yy opioid Programming 7 03-17-2005 03:22 PM
using a perl regular expression in php markus1982 Programming 5 11-18-2002 02:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration