<?php
/**
* Limits output of URL parameters for short-lived tasks with graceful failure handling.
*
* @param array $params Array of URL parameters.
* @param string $task_name Name of the task.
* @return string|false Output string or false on failure.
*/
function limitUrlParams(array $params, string $task_name): string|false
{
$output = '';
$max_params = 5; // Limit the number of displayed parameters.
if (empty($params)) {
return ''; // Return empty string if no parameters are provided.
}
$count = 0;
foreach ($params as $key => $value) {
if ($count >= $max_params) {
// Stop displaying parameters once the limit is reached.
break;
}
// Sanitize the key and value for output.
$sanitized_key = htmlspecialchars($key);
$sanitized_value = htmlspecialchars($value);
$output .= "$sanitized_key=$sanitized_value ";
$count++;
}
if (empty($output)) {
return "No parameters to display for task: $task_name";
}
return "Task '$task_name' parameters (limited): $output";
}
// Example usage:
$params1 = ['param1' => 'value1', 'param2' => 'value2', 'param3' => 'value3', 'param4' => 'value4', 'param5' => 'value5', 'param6' => 'value6'];
$result1 = limitUrlParams($params1, 'short_task');
echo $result1 . "\n";
$params2 = ['param1' => 'value1', 'param2' => 'value2'];
$result2 = limitUrlParams($params2, 'another_short_task');
echo $result2 . "\n";
$params3 = [];
$result3 = limitUrlParams($params3, 'empty_task');
echo $result3 . "\n";
?>
Add your comment