|
Posted by Csaba Gabor on 07/15/06 20:16
frizzle wrote:
> I have a function which validates a string using preg match.
> A part looks like
>
> if( !preg_match( '/^([a-z0-9]+(([a-z0-9_-]*)?[a-z0-9])?)$/', $string )
> ||
> preg_match( '/(--|__)+/' ,$string) ) {
>
> i wonder how i could combine those two into one ...
> I tried a few different options of putting the second match into the
> first one,
> using things like [^__]+ etc, but nothing worked for me.
> it should prevent double (or more) dashes or underscores behind each
> other.
> hello-there = ok
> hello--there != ok
Is hello-_there ok?
Is hello_-there ok?
Is _hello-there ok?
If the answer to the above three questions is no, then the following
should do the trick. Note that this implies that the final character
could be a - or _:
if (preg_match('/^([a-z0-9][-_]?)+$/', $string)) { ... }
Csaba Gabor from New York
Navigation:
[Reply to this message]
|