Last active
September 19, 2019 10:32
-
-
Save arnisjuraga/aa6eb6968cd7a64045d0a49fae1094de to your computer and use it in GitHub Desktop.
Search, is array strings matches suffix of another string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Credits: https://stackoverflow.com/a/10872110/1720476 | |
e.g. I need to search, if given string suffix characters (starts with) "allowed" characters. | |
$allowed = [ 'LV', 'LT' , 'IBAN' ]; | |
$given = 'LViban8500'; | |
*/ | |
$given = 'LViban8500'; | |
$searchIfStringsuffixIsAllowed = function($suffix) use ($given) { | |
return strpos($given, $suffix); | |
}; | |
//now, check all in one line: | |
$hasAllowedSuffix = array_filter(array_map( $searchIfStringsuffixIsAllowed, $allowed), function ($firstchar) { | |
return $firstchar === 0 ? true : false ; } | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Used on small arrays. Performance issues isnot considered.
This will work badly on large arrays, because will not stop "on first match". Improvements needed.