Skip to content

Instantly share code, notes, and snippets.

@zuhairkareem
Last active November 3, 2024 03:12
Show Gist options
  • Save zuhairkareem/a8d7696663ad716adcf18ff5607a8ec8 to your computer and use it in GitHub Desktop.
Save zuhairkareem/a8d7696663ad716adcf18ff5607a8ec8 to your computer and use it in GitHub Desktop.
Search partial string in an array in PHP
<?php
/**
* First Method.
*/
function array_search_partial($arr, $keyword) {
foreach($arr as $index => $string) {
if (strpos($string, $keyword) !== FALSE)
return $index;
}
}
$array = ['apple', 'orange', 'pineapple'];
// search
array_search_partial($array, 'app');
/******************************************* BREAK *****************************************************/
/**
* Second Method.
*/
// $m_array contains matched elements of array.
$m_array = preg_grep('/^app\s.*/', $array);
/******************************************* BREAK *****************************************************/
/**
* Third Method.
*/
$results = array();
foreach ($array as $value) {
if (strpos($value, 'app') !== false) { $results[] = $value; }
}
if( empty($results) ) { echo 'No matches found.'; }
else { echo "'app' was found in: " . implode('; ', $results); }
/******************************************* BREAK *****************************************************/
/**
* Fourth Method.
*/
$results = array_filter($array, function($value) {
return strpos($value, 'app') !== false;
});
/******************************************* BREAK *****************************************************/
/**
* Fifth Method.
*/
array_walk($arr, function($item, $key) {
if(strpos($item, 'green') !== false) {
echo 'Found in: ' . $item . ', with key: ' . $key;
}
});
/******************************************* BREAK *****************************************************/
/**
* Approximate search ; Search almost equal - manual method - rough
*/
foreach ($arr as $index => $string) {
// Split the string to char array.
$keyword_array = str_split($keyword);
// Number of characters matched in City string.
$no_char_matched = 0;
// Check if each char is present in the City.
foreach ($keyword_array as $key=>$value) {
if (stripos($string, $value) !== FALSE) {
// Increment for each matched char.
$no_char_matched++;
}
}
// If the matched char no is more than previous attempts, then store it.
if ($no_char_matched > $max_no_char_matched) {
$max_no_char_matched = $no_char_matched;
// store the city index.
$result = $index;
}
}
/******************************************* BREAK *****************************************************/
/**
* Approximate search ; Search almost equal - manual method - recommended
*/
// input misspelled word
$input = 'carrrot';
// array of words to check against
$words = array('apple','pineapple','banana','orange',
'radish','carrot','pea','bean','potato');
// no shortest distance found, yet
$shortest = -1;
// loop through words to find the closest
foreach ($words as $word) {
// calculate the distance between the input word,
// and the current word
$lev = levenshtein($input, $word);
// check for an exact match
if ($lev == 0) {
// closest word is this one (exact match)
$closest = $word;
$shortest = 0;
// break out of the loop; we've found an exact match
break;
}
// if this distance is less than the next found shortest
// distance, OR if a next shortest word has not yet been found
if ($lev <= $shortest || $shortest < 0) {
// set the closest match, and shortest distance
$closest = $word;
$shortest = $lev;
}
}
echo "Input word: $input\n";
if ($shortest == 0) {
echo "Exact match found: $closest\n";
} else {
echo "Did you mean: $closest?\n";
}
// http://php.net/manual/en/function.levenshtein.php
@RenanLazarotto
Copy link

Thank you so, so, so much! The first method worked like a charm for what I needed. You've just made my life a LOT easier!

@janstieler
Copy link

Hi, Thanks for alle the methods. The first works also to find a key with a simple adjustment.

@kwsim539
Copy link

kwsim539 commented Jan 7, 2021

The first method was just what I was looking for. Kudos!! and Thank you!

@refniD
Copy link

refniD commented May 19, 2022

Thankyou so much, it's really help me ...

@ingobaab
Copy link

ingobaab commented Nov 3, 2024

Which one has the best performance (in php8+)? That would be a nice information. I guess the preg-variant is the slowest..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment