Jump to content

nothing but alphanumerics and forwardslashes via preg_match()? pls help


thepip3r

Recommended Posts

i feel dumb asking this as i've been looking at regex sites and can't figure out why I can't get this to work but anyways, please help where you can.  I'm parsing the URL passed with URL redirection so it has alphanumerics characters, forward slashes (/), etc.  What I'm trying to use regex to do is to filter out anything not alphanumerical or a /.  This is what I've tried:

 

if (preg_match('^([0-9a-zA-Z]+)$', $_GET['arg'])) {
			echo $_GET['arg'];
		} else {
			errorHandler("invalid url rewrite argument");
			echo "FAIL!";
		}

What do you mean by "filter out"?  Filter means you want to take $x and add/remove stuff to it to come out with $y.  Your code looks like you are wanting to validate, not filter.  As in, $x isn't this certain format in the first place, I want to do something.  If you're going for the filter, you would use preg_replace

 

Either way, the only thing I see wrong is that you forgot to put the forward slash in your character class (the [...]) so it's not allowing for the forward slashes. 

 

Another thing that may or may not be a factor is the + makes your preg_match expect at least 1 character match, or it will fail.  If you are expecting $_GET['arg'] to be empty at times, use * instead. 

 

Also,  Unless you are in fact trying to filter (and therefore use preg_replace),  you don't need the parenthesis ( )  they are meant for capturing results for later use. 

 

 

*sigh I don't know why regex is so confusing to me.  Let me try again.  As I stated in my example, I'm trying to parse my URL Rewrite. so when someone types in...

 

www.example.com/myInput/abc123

 

with how my Rewrite is setup, $_GET['arg'] will have "abc123" in it.  What I want my preg_match to TEST for is if $_GET['arg'] has anything else in it besides abc's, 123's, and/or "/". 

 

I've been: http://regexlib.com/ and http://www.regular-expressions.info and still can't figure it out which is why I'm here.  I hope I've re-explained this a bit cleared -- sorry for the confusion.  TIA.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.