3v4l.org

run code in 300+ PHP versions simultaneously
<?php # https://stackoverflow.com/a/32451875/429071 # Will accept any string along as it _also_ contains a single uppercase or number character $str = "Has a single uppercase character only."; $pattern = '/[A-Z0-9]/'; checkStringForPattern($str, $pattern); # https://stackoverflow.com/a/32451873/429071 # Will accept any string as long as it contains _either_ an uppercase or number character $str = "WILLACCEPTTHISSTRING"; $pattern = '/^[A-Z0-9]+$/'; checkStringForPattern($str, $pattern); $str = "0123456789"; $pattern = '/^[A-Z0-9]+$/'; checkStringForPattern($str, $pattern); # The following regex will need *both* types $pattern = "/^(?=.*[A-Z])(?=.*[0-9])[A-Z0-9]*$/"; $str = "WILLFAILBECAUSEITONLYCONTAINSLETTERS"; checkStringForPattern($str, $pattern); $str = "0123456789"; checkStringForPattern($str, $pattern); function checkStringForPattern($str, $pattern){ if(preg_match($pattern, $str)){ var_dump("The [$pattern] pattern accepts the [$str] string."); }else{ var_dump("The [$pattern] does NOT accept the [$str] string."); } }

preferences:
37.79 ms | 901 KiB | 5 Q