Skip to content

Instantly share code, notes, and snippets.

@arnisjuraga
Last active September 19, 2019 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnisjuraga/aa6eb6968cd7a64045d0a49fae1094de to your computer and use it in GitHub Desktop.
Save arnisjuraga/aa6eb6968cd7a64045d0a49fae1094de to your computer and use it in GitHub Desktop.
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
Copy link
Author

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