Results 1 to 6 of 6
Thread: Regex and preg_match() issues
-
07-27-2009, 06:17 PM #1Web Hosting Master
- Join Date
- Aug 2008
- Location
- Vancouver, Canada
- Posts
- 653
Regex and preg_match() issues
I am currently working on a script for a client and I am having some issues with preg_match(). I am trying to validate the form field to prevent the use of invalid characters (characters that are not numerical or alphabetical).
Sample code...
Code:<?php if(!preg_match('/[a-zA-Z0-9]/',$somestring)){ print "ok"; } else { print "not ok"; } ?>
Code:$somestring = "!@hello";
Code:$somestring = "hello!@";
Thanks,
Jay█ Tailored VPS offers fully customizable VPS Hosting
█ Powered by OpenVZ | Servers located in the USA | 99.9% Uptime
-
07-27-2009, 07:14 PM #2Retired Moderator
- Join Date
- Feb 2005
- Location
- Australia
- Posts
- 5,849
You're looking for a word character in the string but you really want to look for a non-word character. Perl regexps have the shortcuts \w for [a-zA-Z0-9] and \W for anything not in that set (non-word characters). Try this:
PHP Code:<?php
if(preg_match('/\W/',$somestring)){
print "not ok";
} else {
print "ok";
}
?>Chris
"Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter
-
07-27-2009, 07:39 PM #3Web Hosting Master
- Join Date
- Nov 2001
- Location
- Vancouver
- Posts
- 2,422
I also agree that using your regex library's "word" metacharacter is a *much* better choice, in the long run. It is shorter than a character class to type and thus perhaps less prone to typos, but more importantly what defines a "word" will vary from one language to another. A simplistic character class a-Z isn't going to cut it if a user quite legitimately enters an accented or non ASCII letter in one of your form fields. Future proof your regexes that might have to deal with Unicode / non ASCII input.
But to answer the OP's question, why didn't it work? Your test wasn't considering the entire string and thus would never see the invalid characters at the end of the string, or anywhere but the first character. Change your code to: /^[a-zA-Z0-9]+$/ and you'll find it does what you expected.
ps: an on-line regex tester that knows about pcre might be useful - here's one from a Google search: http://www.quanetic.com/Regex“Even those who arrange and design shrubberies are under
considerable economic stress at this period in history.”
-
07-27-2009, 08:20 PM #4Web Hosting Master
- Join Date
- Aug 2008
- Location
- Vancouver, Canada
- Posts
- 653
Thanks for the advice! The script is now working properly.
Do you know any websites that has tutorials on Regex?
Thanks again,
Jay█ Tailored VPS offers fully customizable VPS Hosting
█ Powered by OpenVZ | Servers located in the USA | 99.9% Uptime
-
07-27-2009, 08:33 PM #5Web Hosting Master
- Join Date
- Sep 2006
- Location
- Cardiff - United Kingdom
- Posts
- 1,569
Hi Jay,
I found this a pretty good guide:
http://www.phpro.org/tutorials/Intro...PHP-Regex.html
In-fact, it's the best I've come across thus far
Thanks,
Tristan
-
07-27-2009, 08:40 PM #6Web Hosting Master
- Join Date
- Aug 2008
- Location
- Vancouver, Canada
- Posts
- 653
█ Tailored VPS offers fully customizable VPS Hosting
█ Powered by OpenVZ | Servers located in the USA | 99.9% Uptime
Similar Threads
-
(paid work) PHP regex guru is needed for very complicated regex requirement
By mrzippy in forum Employment / Job OffersReplies: 2Last Post: 03-30-2006, 05:10 PM -
PHP: preg_match
By fozzy in forum Programming DiscussionReplies: 10Last Post: 06-30-2005, 02:40 PM -
php preg_match
By getbusy in forum Programming DiscussionReplies: 8Last Post: 09-14-2004, 06:18 PM -
preg_match(); help please!!
By fkdean in forum Programming DiscussionReplies: 1Last Post: 05-13-2004, 04:29 AM -
preg_match
By GaleForce in forum Programming DiscussionReplies: 3Last Post: 02-29-2004, 02:35 PM