regular expressions help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

regular expressions help

Post by Sevengraff »

I'm not even sure if I need to use regular expressions, but they seems like they would work.

I need to check a string and make sure it has only letters, numbers, or under score ( _ ). The string is going to be a filename, so I figured it would be good to check for no slashes or other bad characters.
I was looking at http://www.php.net/manual/en/pcre.pattern.syntax.php but i've never used regular expressions, so I wondering if anyone could help?

this is what i tried:

Code: Select all

$string = 'i_am_good';
if(!eregi("[a-z][0-9][._]", $string)) {
	echo 'bad';
} else {
	echo 'good';
}
but it always returns bad. I looked around for a function that would do this for me, but didn't find anyting.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Re: regular expressions help

Post by m3mn0n »

Code: Select all

<?
$string = 'i_am_god';
if(eregi("_", $string) OR eregi("[[]]", $string)) {
          echo 'good';
} else {
          echo 'bad';
}
?>
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

I'm a newbie to regular expressions. >_<

Slowly but surely, I'll understand it. I just don't get the whole [[:space:]]/[[:alphnum:]], etc.
Image Image
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Sami's code doesn't quite work. It checks for all alphanumerics or a single underscore.

Youre code doesn't quite work because its checking for a string that is exactly 3 characters long. The first is a character a-z, the second is 0-9 and the final is either a period or an underscore.

The following is my first pass. I'm not in a place where I can get to my interpreter right now but it might give you a starting point.

Code: Select all

<?php
if (ereg('^[A-Za-z0-9_][-A-Za-z0-9_]*(\.[A-Za-z0-9])?$',$string))
  echo "Good";
else
 echo "Bad";
?>
First. the enter regexp is surrounded with ^$ which "anchors" the string to the first and last character of the line, ie the entire string must be matched with nothing left over.

Then I require a single alphanumber or an underscore. (Depending on your OS a 0-9 may or may not be legal as a first character and a hyphen isn't in any I know of. Then zero of more other alphanumberics, hyphens, or underscores. Then I'm including a single option extension, which is a single period followed by alphanumberics.

This isn't a perfect regexp for all legal filenames, but it might be good enough depdning on what you need. (For instance a legal filename that fails is foo_bar.tar.gz, because of the multiple extensions.)
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

phice wrote:I'm a newbie to regular expressions. >_<

Slowly but surely, I'll understand it. I just don't get the whole [[:space:]]/[[:alphnum:]], etc.
Those are character classes and are often "shortcuts". For instance [[:alnum:]] is basically equal to [A-Za-z0-9]* That is it matches an arbitrary number of letters and numbers.

Space is basically space, horizontal tab, vertical tab, newline, linefeed, etc.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Oops! I misread your post. :?
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

Wow, thanks guys. Sorry for the delay, I was away for the weekend. I found that it works when i use a double quote instead of the single quote nielsene posted. This works great:

Code: Select all

if (ereg("^[A-Za-z0-9_][-A-Za-z0-9_]*(\.[A-Za-z0-9])?$",$string)) {
	echo "Good";
} else {
	echo "Bad";
}
Post Reply