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.
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:
<?php header('content-type:text/html;charset=utf-8'); $str = ' #123456# '; echo trim($str);//#123456# echo trim($str,#);// 123456 ?>
But when using the default special symbols other than spaces, you need to use double quotes and cannot use single quotes
For example:
<?php header('content-type:text/html;charset=utf-8'); $str=' \t123456 '; echo trim($str);//\t123456 $st = " \t123456 "; echo trim($st);//123456 ?>
Any characters except the beginning and the end cannot be removed
For example:
<?php header(content-type:text/html;charset=utf-8); $str = " 123 456 "; echo trim($str);//123 456 ?>
2. ltrim--delete the blank characters (or other characters) at the beginning of the string
The syntax is the same as trim
For example:
<?php header('content-type:text/html;charset=utf-8'); $str = " \n123 456 "; echo ltrim($str);//123 456 $str = "# 123 456#"; echo ltrim($str,'#');// 123 456# echo ltrim($str,' #');//123 456# ?>
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:
<?php header(content-type:text/html;charset=utf-8); $str = " #123 456\n "; echo rtrim($str);// #123 456 $str = " #123 456 #"; echo rtrim($str,' #');// #123 456 ?>
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:
<?php header('content-type:text/html;charset=utf-8'); $str = "#123456#"; echo str_replace('#','',$str);//123456 ?>
When you want to replace multiple characters, you need to use an array to replace them
<?php header('count-type:text/html;charset=utf-8'); $str = " #123 #456 "; echo str_replace(array(' ','#'),array('',''),$str);//123456 ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
