How to check if a string is in an array?

I need a function to check if there are string characters in the array (each character).

My code is not working yet, but 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 him to say, "Yes, I did." if one of the letters in $ input is found in $allowedChars. Simple enough, right? Well, this does not work, and I have not found a function that will look for a string of individual characters for a value in an array.

By the way, I want these to be just these array values, I'm not looking for fancy html_strip_entities or anything else, I want to use this exact array for valid characters.

+5
source share
5 answers

preg_match: http://php.net/manual/en/function.preg-match.php

:

$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;
+6

? , , - .

preg_match(): http://php.net/manual/en/function.preg-match.php

, ( ):

$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-zA-Z0-9 @._ + -]" " ", * ' , " ", , , "$" .

+4

:

$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\ find out what: <br />';
    print_r($foundTheseOnes);
}
+1

.
preg_match() - / .

: ()

$input="Test Test Test Test";
if(preg_match('/^[\w +.@_-]*$/',$input)){
    echo "Input string does not contain any disallowed characters";
}else{
    echo "Input contains one or more disallowed characters";
}
// output: Yes, input contains only allowed characters

:

/          # start pattern
^          # start matching from start of string
[\w +.@-]  # match: a-z, A-Z, 0-9, underscore, space, plus, dot, atsign, hyphen
*          # zero or more occurrences
$          # match until end of string
/          # end pattern

:

  • ^ $ .
  • \w (aka " " → a ) - : [a-zA-Z0-9_]
  • . " ()" , . .
  • (\-), . /, , .
    , [.-z] , "" z ascii.
  • *, , quantifier ". " 0 " . , preg_match() . , +, " 1 " . , , .
    • {8} , 8 .
    • {4,} , 4 .
    • {,10} , 0 10.
    • {5,9} , 5 9 .

, . ( , , ), , .

  • $allowedChars , , . , array_unique() .
  • str_split($input) . , $input="Test Test Test Test";, str_split() 19 , 14 .
  • , , str_split(), count_chars($input,3) str_split() , , str_split(), array_unique() .
+1

, . preg_match() PCRE .

strcspn() ...

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

0

All Articles