How can a PHP function be written to determine if a string is a "Query"?
To determine if a string is a "Query," we can check if the string starts with a question mark '?' followed by key-value pairs separated by '&'. We can achieve this by using regular expressions in PHP to match the pattern of a query string.
function isQueryString($string) {
return preg_match('/^\?.+=.+(?:&.+=.+)*$/', $string);
}
// Example usage
$string1 = "?key1=value1&key2=value2";
$string2 = "key1=value1&key2=value2";
var_dump(isQueryString($string1)); // Output: int(1)
var_dump(isQueryString($string2)); // Output: int(0)
Keywords
Related Questions
- How can a custom session handler be implemented in PHP for better session management?
- What are some resources or documentation that can help beginners improve their understanding of PHP for handling email addresses?
- What is the difference between str_replace and preg_replace in PHP when it comes to using regular expressions?