How to implement replacement with php regular expressions
php正则表达式实现替换的方法:首先创建一个PHP示例文件;然后定义一个字符串;最后通过“preg_replace($pattern, $replacement, $string);”方式实现字符串替换即可。
推荐:《PHP视频教程》
字符串的替换是字符串操作中非常重要的内容之一。对于一些比较复杂的字符串替换操作,可以通过正则表达式的替换函数 preg_replace() 来完成。
PHP 中的 preg_replace() 函数可以执行正则表达式的搜索和替换,是一个强大的字符串替换处理函数,该函数的语法格式如下:
preg_replace($pattern, $replacement, $subject [, $limit = -1 [, &$count]])
参数说明如下:
$pattern:要搜索的模式,可以使一个字符串或字符串数组;
$replacement:用于替换的字符串或字符串数组。如果这个参数是一个字符串,并且 $pattern 是一个数组,那么所有的模式都使用这个字符串进行替换。如果 $pattern 和 $replacement 都是数组,每个 $pattern 使用 $replacement 中对应的元素进行替换。如果 $replacement 中的元素比 $pattern 中的少,多出来的 $pattern 使用空字符串进行替换。
$subject:要进行搜索和替换的字符串或字符串数组,如果 $subject 是一个数组,搜索和替换回在 $subject 的每一个元素上进行, 并且返回值也会是一个数组。
$limit:可选参数,每个模式在每个 $subject 上进行替换的最大次数。默认是 -1(无限)。
$count:可选参数,如果指定,将会被填充为完成的替换次数。
如果 $subject 是一个数组,preg_replace() 函数会返回一个数组,其他情况下返回一个字符串。
如果函数 preg_replace() 搜索到匹配项,则会返回被替换后的 $subject,否则返回没有改变的 $subject。preg_replace() 函数的每个参数(除了参数 $limit)都可以是一个数组。如果参数 $pattern 和参数 $replacement 都是数组,那么该函数将以其键名在数组中出现的顺序来进行处理。如果发生错误,则返回 NULL。
参数 $replacement 中可以包含后向引用 \\\\\\\\n 或 $n,语法上首选后者。每个这样的引用将被匹配到的第 n 个捕获子组捕获到的文本替换。n 可以是 0-99,\\\\\\\\0 和 $0 代表完整的模式匹配文本。
捕获子组的序号计数方式为:代表捕获子组的左括号从左到右,从 1 开始数。如果要在 $replacement 中使用反斜线,必须使用 4 个("\\\\\\\\\\\\\\\\" 因为这首先是 php 的字符串,经过转义后是两个,再经过正则表达式引擎后才被认为是一个原文反斜线)。
当在替换模式下工作并且后向引用后面紧跟着需要是另外一个数字(比如:在一个匹配模式后紧接着增加一个原文数字),不能使用 \\\\\\\\1 这样的语法来描述后向引用。比如,\\\\\\\\11 将会使 preg_replace() 不能理解你希望的是一个 \\\\\\\\1 后向引用紧跟一个原文 1,还是一个 \\\\\\\\11 后向引用后面不跟任何东西。这种情况下解决方案是使用 ${1}1。这创建了一个独立的 $1 后向引用,一个独立的原文 1。
当使用被弃用的 e 修饰符时,这个函数会转义一些字符(即:'、"、\\\\ 和 NULL)然后进行后向引用替换。当这些完成后请确保后向引用解析完后没有单引号或双引号引起的语法错误(比如:'strlen(\\\\'$1\\\\')+strlen("$2")')。确保符合 PHP 的字符串语法,并且符合 eval 语法。因为在完成替换后,引擎会将结果字符串作为 php 代码使用 eval 方式进行评估并将返回值作为最终参与替换的字符串。
【示例】使用 preg_replace() 函数替换字符串。
<?php $string = 'c biancheng net'; $pattern = '/(\\\\w+).(\\\\w+).(\\\\w+)/i'; $replacement = 'http://$1.$2.$3/php/'; echo preg_replace($pattern, $replacement, $string); ?>
运行结果如下:
http://c.biancheng.net/php/
The above is the detailed content of How to implement replacement with php regular expressions. 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
