Home > Article > Backend Development > PHP program to remove Chinese and English spaces at the beginning and end of a string_PHP tutorial
The following article summarizes several PHP program examples for removing Chinese and English spaces at the beginning and end of strings. Here, regular replacement and trim series functions are used to delete them. Let’s take a look.
Example 1.trim function removes spaces
The trim() function is used to remove spaces at the beginning and end of a string, and returns the string with the spaces removed. The syntax is as follows:
string trim(string str[,string charlist]);
The ltrim() function is used to remove spaces on the left side of a string or specify a string. The syntax is as follows:
string ltrim(string str[,string charlist]);
The rtrim() function is used to remove spaces and special characters on the right side of a string. The syntax is as follows:
string rtrim(string str[,string charlist]);
The code is as follows
|
Copy code
|
||||||||
$a="(a,b,c,)"; echo $a." "; //Output: (a,b,c,) $b=trim($a,"()"); //Remove the characters "(" or ")" at the beginning and end of the string echo $b." "; //Output: a,b,c, $c=trim($a,"(,)"); //Remove the characters "(", "," or ")" at the beginning and end of the string echo $c.""; //Output: a,b,c ?>
Output result: (a,b,c,)
|
a,b,c