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.

Cele mai bune emulatoare pentru console de jocuri pentru Linux
Acest articol va enumera programele populare de emulare a consolei de jocuri disponibile pentru Linux. Emularea este un strat de compatibilitate softw...
Best Linux Distros for Gaming in 2021
The Linux operating system has come a long way from its original, simple, server-based look. This OS has immensely improved in recent years and has no...
Cum să capturați și să transmiteți în flux sesiunea de jocuri pe Linux
În trecut, jocurile erau considerate doar un hobby, dar cu timpul industria jocurilor a cunoscut o creștere imensă în ceea ce privește tehnologia și n...