<?php
/**
* Sanitizes a list of URLs for exploratory work via CLI.
*
* @param array $urls An array of URLs to sanitize.
* @return array A sanitized array of URLs.
* @throws InvalidArgumentException If input is not an array.
*/
function sanitizeUrls(array $urls): array
{
// Validate input
if (!is_array($urls)) {
throw new InvalidArgumentException("Input must be an array of URLs.");
}
$sanitizedUrls = [];
foreach ($urls as $url) {
// Trim whitespace from the URL
$url = trim($url);
// Validate URL format (basic check)
if (!filter_var($url, FILTER_VALIDATE_URL)) {
// Skip invalid URLs or log an error. Here, we'll skip.
continue;
}
// Encode special characters for safe use in various contexts (e.g., database queries)
$sanitizedUrl = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
$sanitizedUrls[] = $sanitizedUrl;
}
return $sanitizedUrls;
}
/**
* CLI interface for URL sanitization.
*/
if (php_sapi_name() === 'cli') {
// Get URLs from command line arguments (excluding script name)
$inputUrls = array_slice($argv, 1);
try {
$sanitizedUrls = sanitizeUrls($inputUrls);
// Output the sanitized URLs
foreach ($sanitizedUrls as $url) {
echo $url . PHP_EOL;
}
} catch (InvalidArgumentException $e) {
echo "Error: " . $e->getMessage() . PHP_EOL;
}
}
?>
Add your comment