Hi
How do I check if a string contains a value in an array ?

The array needs to be a simple list of bad characters ( []*&! ) etc.
$string =" Hello World !";

How do I check $string to see if it contains a character in the 'bad' array ??

Thanks

    One possible approach:

    $chars = preg_quote(implode('', $arrayOfBadCharacters));
    if(preg_match('/['.$chars.']/', $textBeingChecked) {
        // bad character(s) found
    }
    

      Also, depending upon what you're doing note that it might be better to look at it from the other direction - making sure the string only contains valid characters.

      It's a lot harder to define every single possible character you don't want as opposed to the (usually) smaller subset of characters that you do want to allow.

        Thanks for your reply.

        I've tried this:

        $arrayOfBadCharacters =array("£","%","^");
        

        How would I add a " or a ' to the array ??

        Any ideas ?
        Thanks

          Mad_T;10992817 wrote:

          How would I add a " or a ' to the array ??

          You'd do so in the same way you would (and already did) add any other element to the array.

          If you're talking about the syntax for including a character used to delimit a string, see the PHP manual page for what a [man]string[/man] is and how to escape such characters.

            Write a Reply...