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

Navigation:

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

ADD TO Progged Progged
ADD TO DEL.ICIO.US Del.icio.
ADD TO DIGG Digg
ADD TO FURL Furl
ADD TO NEWSVINE Newsvine
ADD TO NETSCAPE Netscape
ADD TO REDDIT Reddit
ADD TO STUMBLEUPON StumbleUpon
ADD TO TECHNORATI FAVORITES Technorati
ADD TO SQUIDOO Squidoo
ADD TO WINDOWS LIVE Windows Live
ADD TO YAHOO MYWEB Yahoo MyWeb
ADD TO ASK Ask
ADD TO GOOGLE Google
ADD TO MAGNOLIA Magnolia
ADD TO SPURL Spurl



 
 

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