3v4l.org

run code in 300+ PHP versions simultaneously
<?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" );
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.24, 7.4.0 - 7.4.13, 8.0.0