Page 1 of 1
regular expressions help
Posted: Fri May 30, 2003 6:42 pm
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.
Re: regular expressions help
Posted: Fri May 30, 2003 7:20 pm
by m3mn0n
Code: Select all
<?
$string = 'i_am_god';
if(eregi("_", $string) OR eregi("[[]]", $string)) {
echo 'good';
} else {
echo 'bad';
}
?>
Posted: Fri May 30, 2003 7:44 pm
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.
Posted: Fri May 30, 2003 7:51 pm
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.)
Posted: Fri May 30, 2003 7:56 pm
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.
Posted: Sat May 31, 2003 2:23 am
by m3mn0n
Oops! I misread your post.

Posted: Mon Jun 02, 2003 2:12 pm
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";
}