views:

65

answers:

4

I basically need a function to check whether a string's characters (each character) is in an array.

My code isn't working so far, but here it is anyway :|

$allowedChars = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"," ","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"," ","0","1","2","3","4","5","6","7","8","9"," ","@",".","-","_","+"," ");

$input = "Test";
$input = str_split($input);

if (in_array($input,$allowedChars)) {echo "Yep, found.";}else {echo "Sigh, not found...";}

I want it to say 'Yep, found.' if one of the letters in $input is found in $allowedChars. Simple enough, right? Well, that doesn't work, and I haven't found a function that will search a string's individual characters for a value in an array.

By the way, I want it to be just those array's values, I'm not looking for fancy html_strip_entities or whatever it is, I want to use that exact array for the allowed characters.

+4  A: 

Are you familiar with regular expressions at all? It's sort of the more accepted way of doing what you're trying to do, unless I'm missing something here.

Take a look at preg_match(): http://php.net/manual/en/function.preg-match.php

To address your example, here's some sample code (UPDATED TO ADDRESS ISSUES IN COMMENTS):

$subject = "Hello, this is a string";
$pattern = '/[a-zA-Z0-9 @./_+-]*/'; // include all the symbols you want to match here

if (preg_match($pattern, $subject))
    echo "Yep, matches";
else
    echo "Doesn't match :(";

A little explanation of the regex: the '^' matches the beginning of the string, the '[a-zA-Z0-9 @./-_+]' part means "any character in this set", the '*' after it means "zero or more of the last thing", and finally the '$' at the end matches the end of the string.

Faisal
Ah, thank you, but when I run that it says that the - before the underscore gives it an error :( why is that? No I am not familiar with RegEx :P, well I have heard of it but I can't do it worth anything :P
Jaxo
This is not what he want. See "if one of the letters in $input is found in". To fix that, remove '^' and '$'.
NawaMan
Hmm, I've done that but it still gives me this error:Warning: preg_match() [function.preg-match]: Unknown modifier '-' in /home/jaxo/web/tests/test.php on line (line with preg_match statement)
Jaxo
how bout using a posix character class match like [:graph:] and from the looks of it [:blank:] or [:space:] for all of them see [wikipedia](http://en.wikipedia.org/wiki/Regular_expression#POSIX_character_classes)
xenoterracide
Right, sorry about that; I just wrote it without thinking too much about the expression. Yes, the '-' is invalid in a character class unless it comes at the beginning or the end (I've moved it to the end in the answer). Sorry for the trouble. :\
Faisal
+2  A: 

You really should look into regex and the preg_match function: http://php.net/manual/en/function.preg-match.php

But, this should make your specific request work:

$allowedChars = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"," ","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"," ","0","1","2","3","4","5","6","7","8","9"," ","@",".","-","_","+"," ");
$input = "Test";
$input = str_split($input);
$message = "Sigh, not found...";
foreach($input as $letter) {
    if (in_array($letter, $allowedChars)) {
        $message = "Yep, found.";
        break;
    }
}
echo $message;
Stephen
+1  A: 

A somewhat different approach:

$allowedChars = array("a","b","c","d","e");
$char_buff = explode('', "Test");
$foundTheseOnes = array_intersect($char_buff, $allowedChars);
if(!empty($foundTheseOnes)) {
    echo 'Yep, something was found. Let\'s find out what: <br />';
    print_r($foundTheseOnes);
}
karim79
A: 

Because you're just validating a string, see preg_match() and other PCRE functions for handling this instead.

Alternatively, you can use strcspn() to do...


$check = "abcde.... '; // fill in the rest of the characters
$test = "Test";
echo ((strcspn($test, $check) === strlen($test)) ? "Sigh, not found..." : 'Yep, found.');

bob-the-destroyer