PHP正则匹配函数之间的区别介绍
本文介绍PHP常用正则匹配函数之间的区别,主要有str_replace、str_ireplace、substr_replace、 preg_replace、preg_match、preg_match_all、preg_quote、preg_split、 ereg_replace、eregi_replace、preg_replace、str_split,当然其中有几个不能使用正则表达式,但因为跟相关正则函数关系暧昧所以都放到一起比较一下,方便参考。
名称 | 支持正则 | 特点 | 备注 |
str_replace | X | 字符串替换函数,大小写敏感 | |
str_ireplace | X | 字符串替换函数,大小写不敏感,支持数组式批量替换 | 感谢网友franci,提醒添加 |
substr_replace | X | 部分替换字符串函数,可以指定位置index | |
preg_replace | Y | 指定匹配模式进行替换,支持子串引用 | 优先使用 |
ereg_replace | Y | 指定匹配模式进行替换,大小写敏感,支持子串引用 | |
eregi_replace | Y | 指定匹配模式进行替换,大小写不敏感,支持子串引用 | |
ereg | Y | 指定模式全文匹配,可以用来匹配判断,或返回匹配数组 | |
preg_match | Y | 指定模式匹配一次退出,可以用来是否匹配判断,或使用返回的匹配数组 | 优先使用 |
preg_match_all | Y | 指定模式全文匹配,一般用来使用返回的匹配数组 | 优先使用 |
preg_split | Y | 指定匹配模式下正则剖分,如果能用最好还是使用explode或str_split | |
str_split | X | 指定长度剖分字符串,默认单个字符剖分成数组 | |
explode | X | 可以指定单个或多个字符剖分字符串,成功则返回数组,例如12345按照34剖分则返回12和5 | |
preg_quote | - | 转义正则表达式字符,意思就是为特殊字符加上反斜线,正则表达式的特殊字符包括:. * ? [ ^ ] $ ( ) { } = ! | : - | |
1、在PHP中有两套正则表达式函数库,功能非常相似:
一套是POSIX(Portable Operating System Interface of Unix )库提供的,函数以ereg_前缀命名,POSIX正则函数库目前已经不再推荐使用。
一套是PCRE(Perl Compatible Regular Expression)库提供的,函数以preg_前缀命名,推荐大家优先使用。在PCRE中,通常将模式表达式(即正则表达式)包含在两个反斜线“/”之间,如“/^w $/”样式。
2、PHP正则表达式作用:匹配提取、匹配替换

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

DeepSeek: In-depth comparison between R1 and V3 versions helps you choose the best AI assistant! DeepSeek already has tens of millions of users, and its AI dialogue function has been well received. But are you confused when facing the R1 and V3 versions? This article will explain the differences between the two in detail to help you choose the most suitable version. The core difference between DeepSeekR1 and V3 version: Features The design goal of the V3 version focuses on complex problem reasoning, deep logic analysis, multi-functional large language model, focusing on scalability and efficiency architecture and parameter reinforcement learning optimization architecture, parameter scale 1.5 billion to 70 billion MoE hybrid Expert architecture, total parameters are as high as 671 billion, each token is activated by 37 billion

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

DeepSeekAI Tool User Guide and FAQ DeepSeek is a powerful AI intelligent tool. This article will answer some common usage questions to help you get started quickly. FAQ: The difference between different access methods: There is no difference in function between web version, App version and API calls, and App is just a wrapper for web version. The local deployment uses a distillation model, which is slightly inferior to the full version of DeepSeek-R1, but the 32-bit model theoretically has 90% full version capability. What is a tavern? SillyTavern is a front-end interface that requires calling the AI model through API or Ollama. What is breaking limit

The cryptocurrency market is booming, and Bitcoin, as a leader, has attracted the attention of many investors. Many people are curious: Do Bitcoin have stocks? The answer is no. Bitcoin itself is not a stock, but investors can indirectly invest in Bitcoin-related assets through various channels, which will be explained in detail in this article. Alternatives to Bitcoin Investment: Instead of investing directly in Bitcoin, investors can participate in the Bitcoin market by: Bitcoin ETF: This is a fund traded on the stock trading market, whose asset portfolio contains Bitcoin or Bitcoin futures contracts. This is a relatively convenient option for investors who are accustomed to stock investments, without having to hold Bitcoin directly. Bitcoin Mining Company Stocks: These companies' business is Bitcoin mining and holding Bitcoin

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

In-depth analysis of the top 15 Bitcoin Escape Index: Market Outlook for 2025 This article deeply analyzes fifteen commonly used Bitcoin Escape Index, among which the Bitcoin Rhodl ratio, USDT current wealth management and altcoin seasonal index have reached the Escape Index in 2024, attracting market attention. How should investors deal with potential risks? Let us interpret these indicators one by one and explore reasonable response strategies. 1. Detailed explanation of key indicators AHR999 coin hoarding indicator: Created by ahr999, assisting Bitcoin fixed investment strategy. The current value is 1.21, which is in the wait-and-see range, so it is recommended to be cautious. Link to AHR999 Escape Top Indicator: A supplement to AHR999 Coin Hoarding Indicator, used to identify the top of the market. The current value is 2.48, this week
