In PHP, how do I validate strings in vanilla PHP?

String validation, PHP validation, validate string, vanilla PHP
This example demonstrates how to validate strings in vanilla PHP by checking if a string is empty, consists only of whitespace, or matches a specific pattern.
<?php function validateString($input) { // Check if the string is empty if (empty($input)) { return "String is empty."; } // Check if the string consists only of whitespace if (ctype_space($input)) { return "String contains only whitespace."; } // Example of a pattern check - string should only contain letters if (!preg_match("/^[a-zA-Z]+$/", $input)) { return "String contains invalid characters."; } return "String is valid."; } // Example usage $testString = "HelloWorld"; echo validateString($testString); ?>

String validation PHP validation validate string vanilla PHP