Say I have..

$apple = "Joe has two bannas + 1 dog";

How do I check $apple to make sure it only contains letters numbers and a underscore. So that..

$apple = "Joe _ P 133";

... is allowed but ...

$apple = "$@() Dfk";

... isnt.

Andrew

    I would use the ereg() function. if(ereg("@", $apple)) { do this;}
    I am not sure how to check for multiple characters is one call of the ereg function though, but that should be enough to get you started.

    Damien.

      Haha, I just posted this!

      Heres the solution I came up with..

      $test = "A";
      
      $bobo = preg_match("/[^a-z,A-Z,0-9]/", $test);
      
      echo $bobo;

      If the script returns a 0, it means that it found nothing but a-z, A-Z, and 0-9, since 0 means 'off' for computers. If it finds somethng, '1' is returned.

        That will let commas through as well as letters and digits. Also, it will let "$@() Dfk" through, because that string contains letters.

        For a regexp that matches strings that only letters, digits, underscores and (judging from your example) spaces:

        /^[\w ]*$/

        Means "the start of the string"
        \w is PCRE shorthand for "letter, digit, or underscore"
        * means "zero or more" of the preceeding
        $ is the "end of the string".
        So in other words the whole expression reads "The entire string, from beginning to end, is nothing except letters, digits, underscores and spaces".

        PS. Also linked to this thread from here.

          $test = "\$@() Dfk";
          
          if (!preg_match("/[^a-z,A-Z,0-9]/", $test))
          	{
          	echo 'Alpha numeric!';
          	}
          else
          	{
          	echo 'Invalid characters!';
          	}
          

          I used that and it worked fine on mine.. By the way, I am also aiming to take out underscores and spaces.. so ya

            Write a Reply...