Jump to content

[SOLVED] ereg and preg_match accepting dollar sign ?


the_shadow

Recommended Posts

Hi all,

 

I'm new to using regex, but basically I just want to verify that a variable contains either letters or numbers and nothing else (had my own function, but thought there would be any easier way of doing this). I've searched around and came up with these expressions:

 

ereg("^[A-Za-z0-9]*$", $test)

preg_match("/[a-zA-Z0-9]{0,}/",$test)

 

 

They both seem to work alright except the only problem I'm having is that both of them accept a dollar sign ($) after a number and before a letter as well as between letters in the $test string.

 

$test = "a324$sfs32sf";

//$test = "a324s$fs32sf"; // also accepts the $ sign

 

if (ereg("^[A-Za-z0-9]*$", $test)) {

  echo "ereg";

}

if (preg_match("/[a-zA-Z0-9]{0,20}/",$test)) {

  echo "pregmatch";

}

 

 

Can anyone offer some suggestions as to why this is happening?

Thank you.

$test = "a324$sfs32sf";

 

echo "hi$bob";

 

will echo:

hi

 

it will also echo a warning on a variable that has not been set with is $bob, if you have your warnings turned on

 

do you see why?

using the $ in double quotes it will try to echo the variable

 

change it to:

$test = 'a324$sfs32sf'

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.