php method to remove special characters: 1. The trim function can remove the first and last symbols; 2. The ltrim function removes the special symbols on the left, which is the beginning of the string; 3. The rtrim function removes the symbols on the right side of the string, which is the string. symbol behind.
How to remove special characters in php:
We have three functions, namely [trim
】, 【 ltrim
】, 【rtrim
】.
1. First, let’s take a look at the actual usage of trim
.
trim
Remove the first and last symbols
string trim(string $str[,string $charlist])
$str="happy\n";//此时必须用双引号 var_dump(trim($str));
Commonly used special symbols include \t
,\n
,\r
,\o
,\xOB
How to remove other symbols please see the code below
$str="happy@";//此时可以用单引号 var_dump(trim($str,'@'));
2,ltrim
It is to remove the special symbols on the left, which is the beginning of the string.
$str="\nhappy";//此时必须用双引号 var_dump(ltrim($str));
General symbols
$str="@happy";//此时可以用单引号 var_dump(trim($str,'@'));
3, rtrim
Remove the symbols on the right side of the string, that is, the symbols at the end of the string.
The usage of rtrim is the same as that of ltrim
The above is the usage of the beginning and the end. If we want to remove the middle symbol, we need to use str_replace.
str_replace
: Yes String replacement
Example
$str="hap\npy"; var_dump(str_replace("\n","",$str));
This is an array if there are multiple symbols
$str=" hap\np y"; var_dump(str_replace(array(" ","\n",)'',$str));
Related learning recommendations:PHP programming from entry to proficiency
The above is the detailed content of How to remove special characters in php?. For more information, please follow other related articles on the PHP Chinese website!