@ 2019-11-16T11:54:32Z <?php
/**
* Finds whether a single character among a list of characters is in a string
*
* @param string $haystack The string where char_list is looked for.
* @param string $char_list Case sensitive set of characters to look for.
* @return bool
*/
function contains_allowed( string $haystack, string $char_list ) : bool
{
if( '' === $char_list ) return true;
if( '' === $haystack ) return false;
return strcspn( $haystack, $char_list ) !== strlen( $haystack );
}
$tests = [
[ 'haystack' => '', 'char_list' => '' , 'expect' => true ],
[ 'haystack' => '', 'char_list' => 'r', 'expect' => false ],
[ 'haystack' => 'r', 'char_list' => '' , 'expect' => true ],
[ 'haystack' => 'r', 'char_list' => 'r+', 'expect' => true ],
[ 'haystack' => 'r+', 'char_list' => 'r+', 'expect' => true ],
[ 'haystack' => 'w', 'char_list' => 'r+', 'expect' => false ],
[ 'haystack' => 'w+', 'char_list' => 'r+', 'expect' => true ],
];
foreach( $tests as $test => $args )
assert(
contains_allowed( $args[ 'haystack' ], $args[ 'char_list' ] ) === $args[ 'expect' ],
"Failed test #$test"
);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.1.25 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
preferences:dark mode live preview
159.98 ms | 402 KiB | 182 Q