How to remove white space from a string in PHP?

How to remove white space from a string in PHP?

Asked on November 1, 2018 in PHP.
Add Comment


  • 1 Answer(s)

    Here the PHP trim() function is used to remove the white space.

         The PHP trim() function is used to remove whitespace including non-breaking spaces, newlines, and tabs from the beginning and end of the string.
    The whitespace which occurs in the middle of the string cannot be removed.

    For example:

    <?php
    $my_str = ' Welcome to Tutorial Republic ';
    echo strlen($my_str); // Outputs: 33
     
    $trimmed_str = trim($my_str);
    echo strlen($trimmed_str); // Outputs: 28
    ?>
    
    Answered on November 1, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.