We have provided a thread and function to remove the white space only at the beginning of the string or end of the string in PHP. To perform this function, we have the following methods in PHP:


Method 1: Using ltrim() Method: The ltrim() method is used to remove the white space from the beginning of the string.

 <?php
    $str = "  Love you rathorji for great tutorial   ";

    //Using ltrim() function   removes whitespaces from  beginning of a string 
    $str = ltrim($str);
  ?>

Output:

Love you rathorji for great tutorial

 Method 2: rtrim() method is used to strip the white area only at the end of the string

  <?php
    $str = "  space removed from end of the string   ";

    //Using ltrim() function   removes whitespaces from  beginning of a string 
    $str = rtrim($str);
    echo $str;
    ?>


output:

space removed from end of the string