fork(1) download
  1. <?php
  2. function isValidColor( $inputColor) {
  3. $len = strlen( $inputColor);
  4. if( $len != 3 && $len != 6) return false; // только длиной 3 или 6
  5. for( $i=0; $i<$len; $i++) {
  6. $code = ord( substr($inputColor, $i, 1));
  7. if( $code >= 97 && $code <= 102) continue;
  8. if( $code >= 65 && $code <= 70) continue;
  9. if( $code >= 48 && $code <= 57) continue;
  10. return false;
  11. }
  12.  
  13. return true;
  14. }
  15.  
  16.  
  17. $tests = array(
  18. 'FFF' => true,
  19. 'FA0123' => true,
  20. 'бла' => false,
  21. '<script>' => false,
  22. '000000' => true,
  23. );
  24.  
  25. $allGood = true;
  26. foreach( $tests AS $test => $expects) {
  27. $result = isValidColor( $test);
  28. if( $result === $expects) {
  29. echo '.';
  30. } else {
  31. printf("\nTest FAILED for \"%s\": expected %s, got %s\n", $test, $expects, $result);
  32. $allGood = false;
  33. }
  34. }
  35.  
  36. if( $allGood) echo "\nAll tests passed!\n";
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
.....
All tests passed!