Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Search, is array strings matches suffix of another string
<?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 ; }
);
@arnisjuraga

This comment has been minimized.

Copy link
Owner Author

arnisjuraga commented Sep 19, 2019

Used on small arrays. Performance issues isnot considered.
This will work badly on large arrays, because will not stop "on first match". Improvements needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.