Forum Moderators: coopster

Message Too Old, No Replies

Simple Regex

Regex with php preg_match

         

Cherewest

7:57 pm on Jun 6, 2007 (gmt 0)

10+ Year Member



function stringCheck($input){
$input = (get_magic_quotes_gpc())? stripslashes($input) : $input;
if(!preg_match("[A-Za-z0-9]",(trim($input)))){
$input="";}
return $input;
}

I am trying to construct the above function to check that
something that contains ONLY upper/lower case letters or numbers.

It should match REggex23 or 125regEX52 etc.

I've tried several patterns

including
^[a-zA-Z0-9]+$
/^\s?[a-zA-Z0-9]+$/ (thanks eelixduppy :-) )

And I've used eregi instead of preg_match

But the function keeps failing.

Thanks for any help

dreamcatcher

9:39 pm on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$string = 'ABC123';

if (!ctype_alnum [php.net]($string))
{
// error
}

dc

Cherewest

11:02 pm on Jun 6, 2007 (gmt 0)

10+ Year Member



Thank you!

Obviously I didn't know about that function. The regex was a pain!

Thanks again

dreamcatcher

6:47 am on Jun 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You`re welcome. The ctype functions are quite useful.

dc