what is the syntax of the TRIM() function in php?
trim(string,charlist)
Parameter | Description |
---|---|
string | Required. Specifies the string to check |
charlist | Optional. Specifies which characters to remove from the string. If omitted, all of the following characters are removed:”\0″ – NULL”\t” – tab”\n” – new line”\x0B” – vertical tab”\r” – carriage return” ” – ordinary white space |
examples of the TRIM() function
Example 1. In this example, we remove characters from both sides of a string (“He” in “Hello” and “d!” in “World”).
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
Example 2. In this example, we remove whitespaces from both sides of a string.
<?php
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
Example 3. In this example, we remove newlines (\n) from both sides of the string.
<?php
$str = "\n\n\nHello World!\n\n\n";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>