<?php
/**
* Flags anomalies in query strings for backward compatibility.
*
* This function checks for potentially problematic query string parameters
* and flags them as anomalies. It avoids asynchronous operations for
* compatibility with older PHP versions.
*
* @param array $query_string An associative array representing the query string.
* @param array $anomaly_rules An array of anomaly rules. Each rule is an array
* with keys 'parameter' and 'regex'.
* 'parameter' is the name of the parameter to check.
* 'regex' is a regular expression to match.
* @return array An array of flagged anomalies. Each anomaly is an associative
* array with keys 'parameter' and 'message'.
*/
function flagQueryStringAnomalies(array $query_string, array $anomaly_rules): array
{
$anomalies = [];
foreach ($anomaly_rules as $rule) {
$parameter = $rule['parameter'];
$regex = $rule['regex'];
if (isset($query_string[$parameter])) {
if (preg_match($regex, $query_string[$parameter])) {
$anomalies[] = [
'parameter' => $parameter,
'message' => "Anomaly detected: Parameter '$parameter' value matches regex '$regex'.",
];
}
}
}
return $anomalies;
}
// Example Usage (replace with your actual query string and rules)
$query_string = [
'param1' => 'value1',
'param2' => '123abc', //Potential anomaly
'param3' => 'valid_value',
'param4' => 'some_value',
];
$anomaly_rules = [
['parameter' => 'param2', 'regex' => '/[a-zA-Z]/'], // Check for non-numeric characters
['parameter' => 'param3', 'regex' => '/^[^a-zA-Z0-9_]+$/'], //Check for non-alphanumeric characters
];
$anomalies = flagQueryStringAnomalies($query_string, $anomaly_rules);
if (!empty($anomalies)) {
echo "Anomalies detected:\n";
foreach ($anomalies as $anomaly) {
echo "- Parameter: " . $anomaly['parameter'] . ", Message: " . $anomaly['message'] . "\n";
}
} else {
echo "No anomalies detected.\n";
}
?>
Add your comment