1. <?php
  2. /**
  3. * Sanitizes a list of URLs for exploratory work via CLI.
  4. *
  5. * @param array $urls An array of URLs to sanitize.
  6. * @return array A sanitized array of URLs.
  7. * @throws InvalidArgumentException If input is not an array.
  8. */
  9. function sanitizeUrls(array $urls): array
  10. {
  11. // Validate input
  12. if (!is_array($urls)) {
  13. throw new InvalidArgumentException("Input must be an array of URLs.");
  14. }
  15. $sanitizedUrls = [];
  16. foreach ($urls as $url) {
  17. // Trim whitespace from the URL
  18. $url = trim($url);
  19. // Validate URL format (basic check)
  20. if (!filter_var($url, FILTER_VALIDATE_URL)) {
  21. // Skip invalid URLs or log an error. Here, we'll skip.
  22. continue;
  23. }
  24. // Encode special characters for safe use in various contexts (e.g., database queries)
  25. $sanitizedUrl = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
  26. $sanitizedUrls[] = $sanitizedUrl;
  27. }
  28. return $sanitizedUrls;
  29. }
  30. /**
  31. * CLI interface for URL sanitization.
  32. */
  33. if (php_sapi_name() === 'cli') {
  34. // Get URLs from command line arguments (excluding script name)
  35. $inputUrls = array_slice($argv, 1);
  36. try {
  37. $sanitizedUrls = sanitizeUrls($inputUrls);
  38. // Output the sanitized URLs
  39. foreach ($sanitizedUrls as $url) {
  40. echo $url . PHP_EOL;
  41. }
  42. } catch (InvalidArgumentException $e) {
  43. echo "Error: " . $e->getMessage() . PHP_EOL;
  44. }
  45. }
  46. ?>

Add your comment