PHP code to Remove White Spaces from beginning, end or both sides of string

There are three PHP trim functions which are used to remove the white spaces from start, end or both sides of a string.
White Spaces include

  • " " (Normal Space Characters)
  • "\t" (Tabs)
  • "\r" (Return Characters)
  • "x0B" (Vertical Tab Characters)
  • "\0" (NULL Bytes)
Following are functions to remove above White Spaces from a string

Function to Remove White Spaces From Beginning of String:-
ltrim();

Can be Used As


$string = ltrim($string);
?>

Function to Remove White Spaces From End of String:-
rtrim();

Can be Used As


$string = rtrim($string);
?>

Function to Remove White Spaces From Beginning and end of String:-
trim();

Can be Used As


$string = trim($string);
?>

So start using it :-)

Posted in |