php

How to Trim Strings in PHP

How to Trim Strings in PHP
The function of trimming strings is to remove the unwanted characters from string data.  String data requires trimming for various purposes in PHP programming. Some use of trimming is removing unnecessary spaces from user data, eliminating newline character from the end of the string data etc.  When the user submits data by using form fields then unwanted spaces can be submitted with the data by mistake. This type of mistake will generate the error when the data is used for validation. So string trimming is essential for many cases. Three functions are used in PHP for string trimming. These are trim(), ltrim() and rtrim(). The use of these functions is shown in this tutorial using examples.

All trimming functions are used to remove any whitespaces from the starting and end of the string. The following characters are considered as whitespace in PHP. The particular character range can be used without these whitespace characters.

“ “        - space character
“\n”     - newline character
“\t”      - tab character
“\r”      - carriage return character
“\0”     - vertical tab character
“\x0B” - null-byte character

Example-1: trim() function

trim() function can take two inputs as argument values. First argument is mandatory and second argument is optional.  If the optional argument value is omitted then the function will remove space from the starting and ending part the value provided by the first argument. In the following example, there are multiple spaces at the starting and the ending of the variable $str1 and trim function is applied for $str1 without the optional parameter.  “\r\n” whitespaces are included in the variable $str2 and “\r\n” is used as second argument value of trim() function. In this case, trim() function will remove all “\r\n” whitespaces from both side of the variable $str2. Range values can be used as second argument value. There are numeric values on both side of the variable $str3 and numeric range 0… 9 is used as second argument value of trim() function to remove numeric part from both side of the variable $str3.

$str1 = "     I like programming   ";
//using trim() function without optional value
echo trim($str1)."
";
$str2 = "\r\nPHP is a popular programming language\r\n";
//using trim() function with “\r\n” as optional value
echo trim($str2, "\r\n")."
";
$str3 = "123 linuxhint.com 890";
//using trim() function with numeric range as optional value
echo trim($str3, "0… 9")."
";
?>

Output:   

Example-2: ltrim() and rtrim() functions

ltrim() and rtrim() functions are identical to trim() function. ltrim() removes whitespaces or other specific characters from the left side and rtrim() removes  whitespaces or other specific characters from the right side of any string value. In the following example, ltrim() and rtrim() are applied on two variables, $Str1 and $Str2 with and without optional value.

$Str1 = "      PHP is a popular language     ";
$Str2 = "02JavaScript is client-side scripting language99";
//using ltrim() function
echo "Output of ltrim() without optional value: ". ltrim( $Str1 )."
";
echo "Output of ltrim() with optional value: ". ltrim( $Str2, "0… 9" )."

";
//using rtrim() function
echo "Output of rtrim() without optional value: ". rtrim( $Str1 )."
";
echo "Output of rtrim() with optional value: ". rtrim( $Str2, "0… 9" )."
";
?>

Output:   

You will need to use trimming functions in your PHP program based on the requirement for getting the appropriate output.  I hope this tutorial will help you to understand the use of trimming functions and apply them properly in your PHP script.

Gry Najlepsze gry w laboratorium aplikacji Oculus
Najlepsze gry w laboratorium aplikacji Oculus
Jeśli jesteś posiadaczem gogli Oculus, musisz wiedzieć o sideloadingu. Sideloading to proces instalowania w zestawie nagłownym treści innych niż sklep...
Gry Top 10 Games to Play on Ubuntu
Top 10 Games to Play on Ubuntu
Windows platform has been one of the dominating platforms for gaming because of the huge percentage of games that are developing today to natively sup...
Gry 5 najlepszych gier zręcznościowych dla systemu Linux
5 najlepszych gier zręcznościowych dla systemu Linux
W dzisiejszych czasach komputery to poważne maszyny używane do gier. Jeśli nie możesz uzyskać nowego wysokiego wyniku, będziesz wiedział, o co mi chod...