Forum Moderators: coopster

Message Too Old, No Replies

Help with preg match

How to only allow a-zA-Z0-9 and ' and "

         

kazisdaman3

8:49 pm on Dec 24, 2006 (gmt 0)

10+ Year Member



I'am having extreme amount of difficulty getting preg_match to work. Can someone please help me here.

I want to only allow a-z, A-Z, 0-9, ' and ".

Thanks in advance, couldn't find solution after many hours!

mcavic

9:05 pm on Dec 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This seems to work:
preg_match("/[A-Za-z0-9\'\"]/", $s)

You want square brackets enclosing the characters and character ranges you're allowing, and backslashes to escape any special characters.

Incidentally, if you wanted to allow only A, Z, and dash, this would work:
preg_match("/[A\-Z]/", $s)

kazisdaman3

9:54 pm on Dec 24, 2006 (gmt 0)

10+ Year Member



thanks a super ton! I'll have to give that a shot, thanks!

kazisdaman3

11:26 pm on Dec 24, 2006 (gmt 0)

10+ Year Member



if (preg_match("/[A-Za-z0-9\'\"]/", $s)
{
echo 'OK';
}
else
{
echo 'NOT OK';
}

--------------

I'm still having trouble, I think this has been greatly confusing me.

if $s = 2342342; that echos OK

if $s = $; that echos NOT OK

but if I add in accepted characters with non accepted characters it always returns ok.

if $s = $23232; that echos OK

any ideas?

Psychopsia

1:09 am on Dec 25, 2006 (gmt 0)

10+ Year Member



Use this:
preg_match("/^[A-Za-z0-9\'\"]$/", $s)

kazisdaman3

3:07 am on Dec 25, 2006 (gmt 0)

10+ Year Member



if preg_match("/^[A-Za-z0-9\'\"]$/", $s) )
{
echo 'OK';
}
else
{
echo 'NOT OK';
}

that seems to echo OK for whatever $s is.

-------------------------------

Maybe, I'am trying to use the wrong function. I only want to allow those characters, but it seems like, that function is just determining if those characters appear in the preg_match. So it will always give me a green light if they appear, I guess.

Is there a function, that will return, IF AND ONLY IF those characters appear. And if another one appears don't accept it.

The only other way I've made it work, is making a preg_match for all the characters to disallow.

But I want it to only allow certain characters, not just tell me whether or not the characters show up. So maybe preg_match, now that I really understand it isn't what I need am I correct here?

kazisdaman3

3:27 am on Dec 25, 2006 (gmt 0)

10+ Year Member



I looked up the ctype_alnum feature because it had an exact one:

preg_match('/^[a-z0-9]+$/iD', $text).

-------------

I assume the difference here is the inclusion of the /iD.

What does that do? I know the i does the in-case sensitve. but the D?

I assume that is different that what we were doing, can someone explain. Thanks! for all the help!

kazisdaman3

3:27 am on Dec 25, 2006 (gmt 0)

10+ Year Member



preg_match('/^[a-z0-9\"\']+$/iD', $text).

eelixduppy

3:46 am on Dec 25, 2006 (gmt 0)




D (PCRE_DOLLAR_ENDONLY)

If this modifier is set, a dollar metacharacter in the pattern matches only at the end of the subject string. Without this modifier, a dollar also matches immediately before the final character if it is a newline (but not before any other newlines). This modifier is ignored if m modifier is set. There is no equivalent to this modifier in Perl.


[us2.php.net...] :)