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

How to remove special characters in PHP?

Guanhui
Release: 2023-03-02 21:12:01
Original
3656 people have browsed it

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])
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

ltrim is to remove the special symbols on the left that 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

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));
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

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!

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