php method to use regular expressions to implement string replacement: first create a PHP sample file; then set the "s (PCRE_DOTALL)" modifier; finally use regular expressions to match the characters in the pattern to uppercase and lowercase letters. .
The operating environment of this tutorial: Windows 7 system, PHP version 5.6. This method is suitable for all brands of computers.
Recommended: "PHP Video Tutorial"
PHP string regular replacement function preg_replace usage instructions
1. preg_replace( )
$msg = preg_replace("/<style>.+<\/style>/is", "", $msg); -----删除<style></style>和中间的部分 $msg = preg_replace("/<[^>]+>/", "", $msg); -----是删除<>和中间的内容 i (PCRE_CASELESS)
If this modifier is set, characters in the pattern will match both uppercase and lowercase letters.
s (PCRE_DOTALL)
If this modifier is set, the dot metacharacter (.) in the pattern matches all characters, including newlines. Without this setting, newline characters are not included. This is equivalent to Perl's /s modifier. Excluded character classes such as [^a] always match newlines, regardless of whether this modifier is set.
2. ereg() and eregi()
Note: The preg_match() function is usually a faster alternative than ereg()
eregi("<body([^>]+)>(.+)</body>",$data,$b)----察看$data中是否有body标签。如果有,把参数赋值$b[0],中间部分赋值$b[1]。 bool ereg ( string pattern, string string [, array regs] ) int eregi ( string pattern, string string, array [regs] )
eregi() and ereg () is similar and has the same usage. The difference is that ereg() is case-sensitive, while eregi() is case-independent.
The above is the detailed content of How to use regex to implement string replacement 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

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools
