Home | Scripts | Tutorials | Books | Hosting | Forums | Links|

Navigation:

Home
Source code
Tutorials
RSS feeds
Articles
Wordpress plugins
Blog
Books
Software
Downloads
Hosting
manuals
Script directory
Training
Our Links






 
 

SPONSORS



 
 

Using trim to get rid of whitespace

This shows how to use the trim function to remove white spaces from either side of a string . This does not remove any white space from inside the string . The format of this function is trim(string); .

What could we use this for , well lets say you wanted a user to enter there name in a text box and then to validate it . There is a chance they could add extra white space before or after their name , this would ensure this was removed.

Code :

<?php
//trimming white space from a string
//our two strings
$string1 = " spaces before";
$string2 = " spaces before and after ";
//remove the whitespace
$trimmed_string1 = trim($string1);
$trimmed_string2 = trim($string2);
//display the text
echo $trimmed_string1;
echo "<br>";
echo $trimmed_string2;
?>

Example :

spaces before
spaces before and after