PHP String trim


Used to remove special characters such as leading and trailing blanks or specified characters.

The correlation functions are as follows:

  • trim(): remove special characters such as leading and trailing blanks or a sequence of characters
  • ltrim(): remove special characters such as string whitespace or specified sequence of characters
  • rtrim(): remove special characters such as string trailing blanks or specific sequences of characters
  • chop(): same as rtrim()

trim()

Removes special characters such as leading and trailing blanks or a sequence of characters.

grammar:

string trim(string str [, charlist])

The trim() function will remove these characters at the beginning and end of the string when the charlist argument is set, otherwise the trim() function will remove the following special characters at the beginning and end of the string:

characters Description
Space
t tab
n line break
r a carriage return
null character
x0B a vertical tab

example:

$text =" Hello World ";
$trimmed = trim($text);
echo $trimmed; // Output "Hello World"
echo " <br /> ";
echo trim($trimmed, " Hdle "); // output " o Wor "
echo " <br /> ";
echo trim($text, " Hdle "); // output " o World "

As you can see from this example, the trim() function will not remove the non-leading charlist.


ltrim()

Removes the special symbol or the specified character sequence from the beginning of the string, in the same way as trim().

grammar:

string ltrim(string str [, charlist])

rtrim()

Removes special characters from the end of a string or a specified sequence of characters, the same as trim().

grammar:

string rrim(string str [, charlist])

chop()

Same with rtrim().




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook