Home / PHP Tips Generated By ChatGPT / Removing Whitespace from Both Ends of a String in PHP Apply trim() to strip whitespace (or other characters) from the beginning and end of a string. Source Code $text = " Hello World! "; $trimmedText = trim($text); echo $trimmedText; // Outputs: "Hello World!"