Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string is in an array?

Tags:

arrays

string

php

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.

like image 616
Scott Avatar asked Jul 20 '10 01:07

Scott


People also ask

How do you check if a string is present in an array Java?

contains() Method: List contains() method in Java is used for checking if the specified element exists in the given list or not.

How do you check if an element is in an array?

The simplest and fastest way to check if an item is present in an array is by using the Array. indexOf() method. This method searches the array for the given item and returns its index. If no item is found, it returns -1.

How do I find a string in an array of strings?

A simple solution is to linearly search given str in array of strings. A better solution is to do modified Binary Search. Like normal binary search, we compare given str with middle string. If middle string is empty, we find the closest non-empty string x (by linearly searching on both sides).

How do you check if a string is present in array of strings in C?

We will use strcmp() to compare the two strings. int strcmp(const char *str1, const char *str2); strcmp() compares string str1 to string str2. The function returns 0 if both the strings are same.


3 Answers

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;
like image 124
Stephen Avatar answered Sep 23 '22 03:09

Stephen


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.

like image 28
Faisal Avatar answered Sep 20 '22 03:09

Faisal


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);
}
like image 39
karim79 Avatar answered Sep 22 '22 03:09

karim79