I'm looking for the PHP version of perls:

tr///

or an alternate method of doing the same thing.

Unfortunately, I've been searching but haven't turned up any matches primarily I suppose because "tr" is not a very searchable phrase, and everything else I've tried is too broad.

What I'm trying to do is simple to verify that a string only contains characters from a certain set, such as in perl I might do:

if ( tr/[a-zA-Z0-9]/cs ) { ... }

The only thing I've found in PHP so far is to preclude the existance of all other characters (which I think would not be a solution)...

Thanks for any help.

Scott

    Sorry, I guess I should ad, I've never been that strong in regular expressions, so chances are there's an easy way to do this and I'm just missing the obvious.

    Scott

      Doh...

      Ignore the question... I'm ashamed I even asked 🙂

        Perl's regular expressions can be used with preg_match().

        You can also do this: if(ereg("[a-zA-Z0-9]+$", $input))...

        If you want to allow empty strings, use "*" instead of "+".

          Thanks Vedran...

          I really should devote some time to Regular Expressions. I have been building them in perl for years and can always get them done, but I'm just not that efficient at building them.

          I ended up using:

          if ( ! ereg("[a-zA-Z0-9]", $input ) )...

          That alerts me if there are any characters outside of that range in the expression.

          I have a separate test to determine if there is a value or not (necessary since I'm generating a different error to the end user based on whether it is empty or has non alpha-numeric characters in it)...

          Scott

            Write a Reply...