thepip3r Posted January 20, 2009 Share Posted January 20, 2009 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!"; } Link to comment https://forums.phpfreaks.com/topic/141618-nothing-but-alphanumerics-and-forwardslashes-via-preg_match-pls-help/ Share on other sites More sharing options...
.josh Posted January 20, 2009 Share Posted January 20, 2009 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. Link to comment https://forums.phpfreaks.com/topic/141618-nothing-but-alphanumerics-and-forwardslashes-via-preg_match-pls-help/#findComment-741291 Share on other sites More sharing options...
thepip3r Posted January 20, 2009 Author Share Posted January 20, 2009 *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. Link to comment https://forums.phpfreaks.com/topic/141618-nothing-but-alphanumerics-and-forwardslashes-via-preg_match-pls-help/#findComment-741343 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.