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

PHP and MySQL/MariaDB tutorial on Ubuntu

PHP and MySQL are the two most important tools to learn today for web programming. PHP is a server side programming language...
28/12/2020

How to Send Email from PHP

For any website, sending email by PHP script is a very common requirement.  You can send email using PHP by using PHP...
28/12/2020

How to read and print pretty JSON with PHP

JSON is a popular data storage format to exchange data between server and browser. It is derived from JavaScript and supported...
29/12/2020
Bài Viết

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

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024