Search for PHP array element containing string
To find values that match your search criteria, you can use array_filter
function:
$example = array('An example','Another example','Last example');
$searchword = 'last';
$matches = array_filter($example, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
Now $matches
array will contain only elements from your original array that contain word last (case-insensitive).
If you need to find keys of the values that match the criteria, then you need to loop over the array:
$example = array('An example','Another example','One Example','Last example');
$searchword = 'last';
$matches = array();
foreach($example as $k=>$v) {
if(preg_match("/\b$searchword\b/i", $v)) {
$matches[$k] = $v;
}
}
Now array $matches
contains key-value pairs from the original array where values contain (case- insensitive) word last.
search a php array for partial string match
You can use preg_grep function of php. It's supported in PHP >= 4.0.5.
$array = array(0 => 'blue', 1 => 'red', 2 => 'green string', 3 => 'red');
$m_array = preg_grep('/^green\s.*/', $array);
$m_array
contains matched elements of array.
Check if string contains a value in array
Try this.
$string = 'my domain name is website3.com';
foreach ($owned_urls as $url) {
//if (strstr($string, $url)) { // mine version
if (strpos($string, $url) !== FALSE) { // Yoshi version
echo "Match found";
return true;
}
}
echo "Not found!";
return false;
Use stristr() or stripos() if you want to check case-insensitive.
Search for partial string in PHP array? Search value contains hashtag
Remove the starting \b
preg_match("/$searchword\b/i", $var);
or
Use \B
preg_match("/\B$searchword\b/i", $var);
Why?
It's all about word boundaries \b
matches between word char and a non-word char or vice-versa. Since the first character is #
which is a non-word character and before that character there exists the start of the line boundary. \B
is the suitable one for this, which matches between two non-word chars or two word chars. Here start of the line , #
so there must exists \B
in-between not \b
.
PHP Check if array element exists in any part of the string
Just loop the array containing the values, and check if they are found in the input string, using strpos
$colors = array("blue","red","white");
$string = "whitewash"; // I want this to be found in the array
foreach ( $colors as $c ) {
if ( strpos ( $string , $c ) !== FALSE ) {
echo "found";
}
}
You can wrap it in a function:
function findString($array, $string) {
foreach ( $array as $a ) {
if ( strpos ( $string , $a ) !== FALSE )
return true;
}
return false;
}
var_dump( findString ( $colors , "whitewash" ) ); // TRUE
Check if string contains any value of an array?
Explode $value
and convert it into array and then use array_intersect()
function in php to check if the string does not contain the value of the array.Use the code below
<?php
$search=array("<",">","!=","<=",">=");
$value='name >= vivek ';
$array = explode(" ",$value);
$p = array_intersect($search,$array);
$errors = array_filter($p);
//Check if the string is not empty
if(!empty($errors)){
echo "The string contains an value of array";
}
else
{
echo "The string does not containe the value of an array";
}
?>
Test the code here http://sandbox.onlinephpfunctions.com/code/7e65faf808de77036a83e185050d0895553d8211
Hope this helps you
Does array element contain substring?
Loop through the $forbiddennames
array and use stripos
to check if the given input string matches any of the items in the array:
function is_forbidden($forbiddennames, $stringtocheck)
{
foreach ($forbiddennames as $name) {
if (stripos($stringtocheck, $name) !== FALSE) {
return true;
}
}
}
And use it like below:
if(is_forbidden($forbiddennames, $stringtocheck)) {
echo "This is a forbidden username.";
} else {
echo "True";
}
Demo!
Search in array with relevance
array_filter
lets you specify a custom function to do the searching. In your case, a simple function that uses strpos()
to check if your search string is present:
function my_search($haystack) {
$needle = 'value to search for';
return(strpos($haystack, $needle)); // or stripos() if you want case-insensitive searching.
}
$matches = array_filter($your_array, 'my_search');
Alternatively, you could use an anonymous function to help prevent namespace contamination:
$matches = array_filter($your_array, function ($haystack) use ($needle) {
return(strpos($haystack, $needle));
});
Best way to find a string in a PHP array?
Easiest Way
$word='admin';
$array = array('sales_agent', 'admin');
if (in_array($word, $array)) {
echo "admin is in array";
}
Related Topics
Get Date Range Between Two Dates Excluding Weekends
Rotate Array Elements to the Left (Move First Element to Last and Re-Index)
How to Get Xdebug Var_Dump to Show Full Object/Array
Export MySQL Database Using PHP
Dependency Hell - How Does One Pass Dependencies to Deeply Nested Objects
How to Sort an Array of Utf-8 Strings
When to Use HTMLspecialchars() Function
File_Get_Contents Receive Cookies
Could Not Instantiate Mail Function. Why This Error Occurring
Naming Convention Issues When Using Codeigniter in Windows and Linux
Find Common Values in Multiple Arrays with PHP
Deleting All Files from a Folder Using PHP
Laravel Socialite: Invalidstateexception
How to Limit File Upload Type File Size in PHP