In PHP, you can remove whitespace (spaces, tabs, and newlines) from the beginning and end of a string using the trim()
function. The trim()
function will remove leading and trailing whitespace characters and return the modified string.
Here’s an example of how to use trim()
to remove whitespace from the beginning and end of a string:
// Original string with whitespace at the beginning and end
$originalString = " Hello, this is a sample string with whitespace. ";
// Using trim() to remove whitespace from the beginning and end of the string
$trimmedString = trim($originalString);
// Output the result
echo "Original string: " . $originalString . "\n";
echo "Trimmed string: " . $trimmedString . "\n";
Output:
Original string: Hello, this is a sample string with whitespace.
Trimmed string: Hello, this is a sample string with whitespace.
As you can see, the trim()
function removes the leading and trailing spaces, resulting in a clean string without any whitespace at the beginning or end.