PHP function trim() to remove leading and trailing spaces and special characters from a string

伊谢尔伦
Release: 2023-03-07 12:16:01
Original
14418 people have browsed it

Usually when users are asked to enter data in forms or other text boxes, they may accidentally enter spaces or other special characters and so on. In some cases, spaces and special characters are not allowed in string. In this case, you need to use something to remove these spaces and special characters. Trim() Function is provided in PHP to remove spaces and special characters on the left and right sides of a string. The ltrim() function removes spaces and special characters on the left side of a string. The rtrim() function removes spaces on the right side of a string. spaces and special characters. These operations can be easily performed using these PHP built-in functions. The following introduces the syntax and usage of these functions.

trim() function

trim() function removes both sides of a string whitespace characters or other special characters, and returns the string after removing the spaces and special characters.

The syntax format is as follows:

 trim(string,charlist)
Copy after login

Parameters:

The parameter string of the trim() function is the string to be operated Object; the parameter charlist is optional The optional parameter specifies which characters need to be deleted from the string. If this parameter is not set, all optional characters will be deleted.

trim() Function parameters The optional values ​​​​of charlist are as follows:

\0NULL
\tTab
\n Line break
\x0BVertical tab character
\rEnter
" "Space
##Use the trim() function to remove whitespace characters on both sides of the string Or other special characters, the example code is as follows:

<?php
$str = "\r\r(:@_@ PHP中文网  @_@:)    ";
echo trim($str);                             // 去除字符串左右两边的空格
echo "<br>";
echo trim($str,"\r\r(: :)");             // 去除字符串左右两边的特殊字符\r\r(: :)
?>
Copy after login

The output result is:

(:@_@ PHP中文网 @_@:)
@_@ PHP中文网 @_@
Copy after login

##ltrim() function ltrim() function removes the spaces and special characters on the left side of the string,

and returns the string after removing the spaces and special characters on the left side.

The syntax format is as follows:

ltrim(string,charlist)
Copy after login

Use the ltrim() function to remove blank characters or other special characters on the left side of the string. The example code is as follows:

<?php
$str = "  (:@_@  PHP中文网  @_@:)    ";
echo ltrim($str);                             // 去除字符串左边的空格
echo "<br>";
echo ltrim($str," (:@_@ ");         // 去除字符串左边的特殊字符(:@_@
?>
Copy after login

The output result is:

(:@_@ PHP中文网 @_@:)
PHP中文网 @_@:)
Copy after login

rtrim() function ##rtrim() function removes spaces and special characters on the left side of the string and returns A string with spaces and special characters on the right removed.

The syntax format is as follows:

rtrim(string,charlist)
Copy after login

Use the rtrim() function to remove blank characters or other special characters on the right side of the string. The example code is as follows:

<?php
$str = "  (:@_@  PHP中文网  @_@:)    ";
echo rtrim($str);                             // 去除字符串右边的空格
echo "<br>";
echo ltrim($str," @_@:) ");         // 去除字符串右边的特殊字符@_@:)
?>
Copy after login

Output results For:

(:@_@ PHP中文网 @_@:)
(:@_@ PHP中文网
Copy after login
[Related recommendations]

1.
Share examples of the TrimHelper method in Java

2. Detailed explanation of Java String.trim() method example

3. How to remove spaces on both sides of a string with trim() in js

4. In Java The trim() function

5. Detailed explanation of usage examples of java trim

The above is the detailed content of PHP function trim() to remove leading and trailing spaces and special characters from a string. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template