How to Trim Strings in PHP

28/12/2020
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

“”     – 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.  “rn” whitespaces are included in the variable $str2 and “rn” is used as second argument value of trim() function. In this case, trim() function will remove all “rn” 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.

<?php
$str1 = "     I like programming   ";

//using trim() function without optional value
echo trim($str1)."<br/>";
$str2 = "rnPHP is a popular programming languagern";

//using trim() function with “rn” as optional value
echo trim($str2, "rn")."<br/>";
$str3 = "123 linuxhint.com 890";

//using trim() function with numeric range as optional value
echo trim($str3, "0..9")."<br/>";
?>

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.

<?php
$Str1 = "      PHP is a popular language     ";
$Str2 = "02JavaScript is client-side scripting language99";

//using ltrim() function
echo "Output of ltrim() without optional value: <b>". ltrim( $Str1 )."</b><br/>";
echo "Output of ltrim() with optional value: <b>". ltrim( $Str2, "0..9" )."</b><br/><br/>";

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

?>

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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

How to Trim Strings in PHP

The function of trimming strings is to remove the unwanted characters from string data.  String data requires trimming...
28/12/2020

Setting up a Debian 10 LAMP Server for PHP Web Development

In this article, I am going to show you how to setup a LAMP (Linux, Apache, MySQL/MariaDB, PHP) server for PHP web development....
29/12/2020

Building a Contact Form in PHP

How to create contact form using HTML, CSS and PHP A contact form is a very essential part of any website. The visitors...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024