In PHP, how do I filter strings in Symfony?

// Example of filtering a string in Symfony use Symfony\Component\Validator\Validation; use Symfony\Component\Validator\Constraints\NotBlank; $validator = Validation::createValidator(); $string = ' Hello World! '; // Trim whitespace using PHP's trim function $filteredString = trim($string); // Validate the trimmed string $violations = $validator->validate($filteredString, [new NotBlank()]); if (count($violations) > 0) { // Handle violations foreach ($violations as $violation) { echo $violation->getMessage(); } } else { echo $filteredString; // Outputs: Hello World! }