You can use the trim() function to remove whitespace or other predefined characters from both sides of a string.
If you want to remove from only the left side, you can use the ltrim() function.
If you want to remove from only the right side, you can use the rtrim() function.
If you do not specify which character needs to be removed, these functions will remove the following characters:
"\0" - NULL
"\t" - tab
"\n" - new line
"\x0B" - vertical tab
"\r" - carriage return
" " - white space
Here is an example:
<?php
$str = "\n hello world \n\n";
echo $str;
echo trim($str);
?>
The above code will return the following output:
hello world
hello world