Home>Article>Backend Development> How to delete special strings in php

How to delete special strings in php

藏色散人
藏色散人 Original
2021-07-12 09:30:19 5130browse

How to delete special strings in php: 1. Use trim to remove the blank characters at the beginning and end of the string; 2. Use ltrim to delete the blank characters at the beginning of the string; 3. Use rtrim to delete the space characters at the end of the string. ;4. Replace and delete through str_replace function.

How to delete special strings in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to delete special strings in php?

Removing special characters from PHP strings

The functions to remove special characters from strings include trim, ltrim, rtrim, str_replace

1, trim--remove characters Blank characters (or other characters) at the beginning and end of the string

Syntax:

String trim(string $str[,string $charlist])

String trim: Returns the string type

string $str: The string to be processed

string $charlist: Optional, the characters to be filtered (once specified, only the specified characters can be removed, and the default ones will not be removed)

For example:

But when using the default special symbols other than spaces, you need to use double quotes and cannot use single quotes

For example:

Any characters except the beginning and the end cannot be removed

For example:

2. ltrim--delete the blank characters (or other characters) at the beginning of the string

The syntax is the same as trim

For example:

3. rtrim--delete the space character (or other characters) at the end of the string

The syntax is the same as trim

For example Bar:

4, str_replace--substring replacement

mixed str_replace(mixed $search,mixed $replace,mixed $subject[,int &$count])

mixed: mixed

mixed $search: find the target value

mixed $ replace: Replacement value of the target value

mixed $subject: Array or string to perform replacement

int &$count: Optional, the number of times the replacement occurs

For example Example:

When you want to replace multiple characters, you need to use an array to replace them

trim, ltrim, rtrim are special characters that can be removed by default

“”,普通空格字符 “\t”,制表符 “\n”,换行 “\r”,回车 “\o”,字节符 “\xoB”,垂直制表符

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to delete special strings 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