Muffin Posted March 9, 2007 Share Posted March 9, 2007 First off, I hate regex! Trying to write a match for any of the following characters; \ / : * ? | > < , " [space] All other characters need to be allowable. No matter how I write the expression, it always comes back as no matches. I was using this, but it's (obviously) completely wrong; $quote = "\""; if (ereg("^([\/:*?|><,$quote ])*$", $input)){ echo "match"; } Could someone point out where I've gone wrong here. Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted March 9, 2007 Share Posted March 9, 2007 Im not sure about ereg but with preg it would be something like: $string = "<test?>"; preg_match ("/[<>\\/:*?,|\"\s]/", $string, $matches); print_r ($matches); if (count($matches) > 0) { # match } Would this not be more useful as a preg_replace though? Not knowing what you are trying to do exactly... preg_replace would string the chars out of your string. Quote Link to comment Share on other sites More sharing options...
Muffin Posted March 9, 2007 Author Share Posted March 9, 2007 Thankyou for that, it seems to work well. Doesn't seem to match on \, but I can work around that. I think I tried something similar to that, but didn't realize you could escape characters inside brackets. Oh, just so you know, it was for a name validation. Quote Link to comment Share on other sites More sharing options...
effigy Posted March 9, 2007 Share Posted March 9, 2007 Doesn't seem to match on \ %[<>\\\/:*?,|\"\s]% An easier way to avoid special (non-letter) characters is to use /\W/, or /\P{L}/ if you're Unicode friendly. It may be easier to establish what you want to allow, instead of what you don't. Quote Link to comment Share on other sites More sharing options...
Muffin Posted March 10, 2007 Author Share Posted March 10, 2007 Ta, that works perfectly. I would have set it up as a whitelist, but I needed to allow every other character... Quote Link to comment Share on other sites More sharing options...
mbtaylor Posted March 10, 2007 Share Posted March 10, 2007 Doesn't seem to match on \ %[<>\\\/:*?,|\"\s]% An easier way to avoid special (non-letter) characters is to use /\W/, or /\P{L}/ if you're Unicode friendly. It may be easier to establish what you want to allow, instead of what you don't. Ive not heard of that (/\W/), im going to have to read your regex post links Effigy Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.