php method to remove the head and tail of a string: 1. Use the ltrim function to delete the blank characters or other characters at the beginning of the string; 2. Use the rtrim function to delete the blank characters or other characters at the end of the string.
The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer
php How to remove the header from a string tail?
PHP string trim ltrim rtrim function to remove the first and last specified characters
Today, we look at a relatively simple string processing function. We all know that during the form submission process, However, the content entered by the user may not necessarily be the same legal data as you think. For example, when entering a user name or email address and leaving a few blank spaces in the input box, the data entering the database will naturally have a few more blank spaces. It's not very logical, so generally we can use trim to remove the effect of leading and trailing spaces.
trim function
Syntax: trim ( string $str [, string $character_mask ] ) : string
Function: Delete the blank characters (or other characters) at the beginning and end of the string
Return value: Return the processed string
The same series of functions include ltrim rtrim function
ltrim — Remove whitespace characters (or other characters) at the beginning of the string
rtrim — Remove whitespace characters (or other characters) at the end of the string
One parameter
Using only one parameter in the function can only remove the specified characters. Without the second parameter, trim() only removes the following characters:
Two parameters
By specifying character_mask, you can Specify a list of characters to delete. Simply list all the characters you want to remove. Or by using the .. format, a range can be specified.
#trim去除前后指定字符列表 $str = "Hello Tacks"; var_dump($str); var_dump(trim($str,'Hes')); #删除 $binary 开头的 ASCII 控制字符 $binary = "\x09Example string\x0A"; // (从 0 到 31,包括 0 和 31) $clean = trim($binary, "\x00..\x1F"); var_dump($clean);
emmm, the other two functions ltrim and rtrim have the same usage, so I won’t repeat them here.
Small example
Did you think it was about to end? You thought this function was very simple. Then take a look at the code below.
$str = "Hello php"; var_dump($str); var_dump(rtrim($str,'hp'));
If you think the processed string is 'Hello p', then you still don't really understand the usage of the second parameter of this function.
First of all, I use the rtrim function to remove specific characters at the end of the string. Secondly, this specific character is the second parameter, which is a character list, so the string is continuously matched from right to left. The characters in the character list will match p and h, so the final result is 'Hello'. So don’t be careless and use the wrong function to achieve the results you want.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove the head and tail of a string in php. For more information, please follow other related articles on the PHP Chinese website!