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

How to remove special characters in PHP?

Guanhui
GuanhuiOriginal
2020-07-22 11:11:453599browse

How to remove special characters in PHP?

How to remove special characters in PHP?

1. The "trim()" function can remove spaces and special characters on the left and right sides of a string;

2. The "ltrim()" function removes spaces and special characters on the left side of a string. Blank characters or other predefined characters;

3. The "str_replace()" function replaces some characters in the string with other characters.

Usage example

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

ltrim is to remove the special symbols on the left that is the beginning of the string

$str="\nhappy";//此时必须用双引号
var_dump(ltrim($str));

General symbols

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

rtrim is the right side of the string, that is, the back of the string

The usage of rtrim is the same as ltrim

The above is the usage of the first and last. If we want to remove the middle symbol, we need to use str_replace.

str_replace: It is 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));

Recommended tutorial: " PHP

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!

Statement:
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