Jump to content

[SOLVED] this should be so simple...


Muffin

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/41930-solved-this-should-be-so-simple/
Share on other sites

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.

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.

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 :)

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.