faqts : Computers : Programming : Languages : PHP : Common Problems : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

17 of 17 people (100%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

Is there a function that ignores white space in a string?

Dec 13th, 2002 01:20
Philip Olson, Tam bouma, Narendra Jain, obi-wan kenobi,


Basically, you can replace the spaces with nothing so let's do that 
using the str_replace() function.

<?php
    $string = "  New York, New York  ";

    $string = str_replace(' ', '', $string);

    // NewYork,NewYork
    print $string;
?>

See also:

    http://www.php.net/str_replace

If you simply wanted to remove the trailing or leading whitespace then 
use trim():

<?php

    $string = " New York, New York ";

    $string = trim($string);

    // New York, New York
    print $string;
?>

It's hard to demonstrate but trust us, the leading and trailing 
whitespace is gone now :)

See also:

    http://www.php.net/trim