SearchEngineChannel

Looking for somewhere to host your site? Browse our PHP web hosts section!

Home > Code Snippets > Remove whitespace from a text string using the PHP trim function

Remove whitespace from a text string using the PHP trim function

Description

If you use a form on your site then you'll know that user input can often have white space before or after the text as the person filling in the form field sometimes accidentally adds spaces. This handy function will remove the whitespace from the beginning or end of the string.

The code


<?php


// The original text string with whitespace at the end
$textString "This is some text with a space after it ";

// Remove the whitespace
$trimmedTextString trim($textString);

// Display the new text string
echo $trimmedTextString;

?>

Get the code

Download the file to your computer: Click here to get the file