Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Different methods to remove spaces from string

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 234
    Comment on it

    This article demonstrates different methods which are used for stripping or removing white space and other specified characters from the string. The list and details of each methods are given below :

     

    1. trim()

    trim() is the most popular function of PHP which is used for removing the white spaces, for example spaces, tabs and new line character, etc. from starting(left side) and ending (right side) of the string. trim() function can also used for removing other predefined characters passed as second optional argument.

     

    Note : trim() can not remove spaces from middle of the string.

     

    Result : It returns the modified (trimmed) string.

     

    Syntax : trim ( $string, $character_list )

    $string : Represents the string to be trimmed.(Required)

    $character_list : Specifies the list of characters to be removed from the string.(Optional)

    Note : Without the second parameter, trim() remove the following characters from start and end of the string :

    " " - White space
    "\n" - New line
    "\t" - Tab
    "\r" - Carriage return
    "\0" - NUL-byte
    "\x0B" - vertical tab

     

    Example :

    1. Without second argument (character list)

    <?php
    $string = " Testing Trim function. ";
    echo "String before trim :".$string;
    echo "<br>";
    echo "String after trim :".trim($string);
    ?>

    Output :

    String before trim : Testing Trim function.
    String after trim :Testing Trim function.

     

    2. With character list

    <?php
    $string = 'Hello Trim';
    echo "String before trim : ".$string;
    echo "<br>";
    echo "String after trim : ".trim($string, 'Heim');
    ?>

    Output :

    String before trim : Hello Trim
    String after trim : llo Tr

     

     

    2. ltrim()

    ltrim() removes the white spaces and specified characters from the beginning (left side) of the string.

     

    Result : Returns the modified string.

     

    Syntax : ltrim( $string, $character_list )

    $string : Represents the string to be trimmed.(Required)

    $character_list : Specifies the list of characters to be removed from the string.(Optional)

     

    Example :

    <?php
    $string = ' Hello Trim';
    echo "String before ltrim :".$string;
    echo "<br>";
    echo "String after ltrim :".ltrim($string);
    echo "<br>";
    echo "String after ltrim with optional argument :".ltrim($string,' H');
    ?>

    Output :

    String before ltrim : Hello Trim
    String after ltrim :Hello Trim
    String after ltrim with optional argument :ello Trim

     

    3. rtrim()

    rtrim() removes the white space or specified characters from the end (right side) of string.

     

    Result : Returns the modified string.

     

    Syntax : rtrim( $string, $character_list )

    $string : Represents the string to be trimmed.(Required)

    $character_list : Specifies the list of characters to be removed from the string.(Optional)

     

    Example :

    <?php
    $string = "hello ";
    echo "Before rtrim : ".$string."World";
    echo "<br>";
    echo "After rtrim : ".rtrim($string)."World";
    echo "<br>";
    echo "String after rtrim with optional argument : ".rtrim($string,'lo ');
    ?>

    Output : 

    Before rtrim : hello World
    After rtrim : helloWorld
    String after rtrim with optional argument : he

     

    Remove spaces from whole string

    trim(), ltrim() and rtrim() functions can only remove white spaces from beginning or ending side of the string. These  functions can not used for removing spaces from middle of the string. To remove spaces from whole string we can use these two tricks :

     

    4. using str_replace()

    str_replace() function replaces all the occurrences of matching characters with the specified characters.

    Example :

    <?php
    $string = " Testing trim function ";
    echo "Before trim : ".$string;
    echo "<br>";
    echo "After trim : ".str_replace(' ', '', $string);
    ?>

    Output :

    Before trim : Testing trim function
    After trim : Testingtrimfunction

     

    5. using preg_replace()

    preg_replace() use regular expression to match the pattern in the string and replace with specified characters or string.

    Example :

    <?php
    $string = " Testing trim function ";
    echo "Before trim : ".$string;
    echo "<br>";
    echo "After trim : ".preg_replace('/\s+/', '', $string);
    ?>

    Output :

    Before trim : Testing trim function
    After trim : Testingtrimfunction

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: