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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.