search
HomeBackend DevelopmentPHP ProblemHow to remove special characters in PHP?

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment