Home > Backend Development > PHP Problem > How to remove special characters in php?

How to remove special characters in php?

coldplay.xixi
Release: 2023-03-02 10:06:01
Original
8841 people have browsed it

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?

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])
Copy after login
$str="happy\n";//此时必须用双引号
var_dump(trim($str));
Copy after login

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,'@'));
Copy after login

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));
Copy after login

General symbols

$str="@happy";//此时可以用单引号
var_dump(trim($str,'@'));
Copy after login

3, rtrimRemove 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));
Copy after login

This is an array if there are multiple symbols

$str=" hap\np y";
var_dump(str_replace(array(" ","\n",)'',$str));
Copy after login

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!

Related labels:
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