Analyzing Ereg regular expressions in PHP_PHP tutorial
We know that Perl is compatible with regular expressions in PHP, so what do we need to know about Ereg regular expressions? Here we introduce Ereg regular expressions to you through analysis of the differences between Perl-compatible regular expressions and Perl/Ereg. We hope it will be helpful to you.
Although it is called "Perl compatible regular expression", compared with Perl's regular expression, PHP's still has some differences. For example, the modifier "G" in Perl means all matches, but in PHP No support for this modifier has been added.
About the analysis of Ereg regular expressions in PHP:
There is also the difference from the ereg series of functions. ereg is also a regular expression function provided in PHP, but Compared with preg, it is much weaker.
1. Separators and modifiers are not required and cannot be used in ereg, so the function of ereg is much weaker than preg.
2. About ".": The dot in the regular expression is usually all characters except the newline character, but the "." in the ereg is any character, including the newline character! If you want "." to include a newline character in preg, you can add "s" to the modifier.
3. ereg uses greedy mode by default and cannot be modified. This brings trouble to many replacements and matchings.
4. Speed: This may be a question that many people are concerned about. Is the powerful function of preg in exchange for speed? Don’t worry, preg is much faster than ereg. The author made a program test:
Ereg regular expression time test example in PHP:
<ol class="dp-c"><li class="alt"><span><span><?php </SPAN></SPAN><LI><SPAN class=func>echo</SPAN><SPAN> </SPAN><SPAN class=string>"Preg_replace used time:"</SPAN><SPAN>; </SPAN><LI class=alt><SPAN class=vars>$start</SPAN><SPAN> = time(); </SPAN><LI><SPAN class=keyword>for</SPAN><SPAN>(</SPAN><SPAN class=vars>$i</SPAN><SPAN>=1;</SPAN><SPAN class=vars>$i</SPAN><SPAN><=100000;</SPAN><SPAN class=vars>$i</SPAN><SPAN>++) { </SPAN><LI class=alt><SPAN class=vars>$str</SPAN><SPAN> = </SPAN><SPAN class=string>"ssssssssssssssssssssssssssss"</SPAN><SPAN>; </SPAN><LI><SPAN>preg_replace(</SPAN><SPAN class=string>"/s/"</SPAN><SPAN>,</SPAN><SPAN class=string>""</SPAN><SPAN>,</SPAN><SPAN class=vars>$str</SPAN><SPAN>); </SPAN><LI class=alt><SPAN>} </SPAN><LI><SPAN class=vars>$ended</SPAN><SPAN> = time()-</SPAN><SPAN class=vars>$start</SPAN><SPAN>; </SPAN><LI class=alt><SPAN class=func>echo</SPAN><SPAN> </SPAN><SPAN class=vars>$ended</SPAN><SPAN>; </SPAN><LI><SPAN class=func>echo</SPAN><SPAN> " </SPAN><LI class=alt><SPAN class=func>ereg_replace</SPAN><SPAN> used time:"; </SPAN><LI><SPAN class=vars>$start</SPAN><SPAN> = time(); </SPAN><LI class=alt><SPAN class=keyword>for</SPAN><SPAN>(</SPAN><SPAN class=vars>$i</SPAN><SPAN>=1;</SPAN><SPAN class=vars>$i</SPAN><SPAN><=100000;</SPAN><SPAN class=vars>$i</SPAN><SPAN>++) { </SPAN><LI><SPAN class=vars>$str</SPAN><SPAN> = </SPAN><SPAN class=string>"ssssssssssssssssssssssssssss"</SPAN><SPAN>; </SPAN><LI class=alt><SPAN class=func>ereg_replace</SPAN><SPAN>(</SPAN><SPAN class=string>"s"</SPAN><SPAN>,</SPAN><SPAN class=string>""</SPAN><SPAN>,</SPAN><SPAN class=vars>$str</SPAN><SPAN>); </SPAN><LI><SPAN>} </SPAN><LI class=alt><SPAN class=vars>$ended</SPAN><SPAN> = time()-</SPAN><SPAN class=vars>$start</SPAN><SPAN>; </SPAN><LI><SPAN class=func>echo</SPAN><SPAN> </SPAN><SPAN class=vars>$ended</SPAN><SPAN>; </SPAN><LI class=alt><SPAN class=func>echo</SPAN><SPAN> " </SPAN><LI><SPAN class=func>str_replace</SPAN><SPAN> used time:"; </SPAN><LI class=alt><SPAN class=vars>$start</SPAN><SPAN> = time(); </SPAN><LI><SPAN class=keyword>for</SPAN><SPAN>(</SPAN><SPAN class=vars>$i</SPAN><SPAN>=1;</SPAN><SPAN class=vars>$i</SPAN><SPAN><=100000;</SPAN><SPAN class=vars>$i</SPAN><SPAN>++) { </SPAN><LI class=alt><SPAN class=vars>$str</SPAN><SPAN> = </SPAN><SPAN class=string>"sssssssssssssssssssssssssssss"</SPAN><SPAN>; </SPAN><LI><SPAN class=func>str_replace</SPAN><SPAN>(</SPAN><SPAN class=string>"s"</SPAN><SPAN>,</SPAN><SPAN class=string>""</SPAN><SPAN>,</SPAN><SPAN class=vars>$str</SPAN><SPAN>); </SPAN><LI class=alt><SPAN>} </SPAN><LI><SPAN class=vars>$ended</SPAN><SPAN> = time()-</SPAN><SPAN class=vars>$start</SPAN><SPAN>; </SPAN><LI class=alt><SPAN class=func>echo</SPAN><SPAN> </SPAN><SPAN class=vars>$ended</SPAN><SPAN>; </SPAN><LI><SPAN>?> </span></span></li></ol>
Ereg regular expression example results in PHP:
<ol class="dp-c"> <li class="alt"><span><span>Preg_replace used time:5 </span></span></li> <li> <span class="func">ereg_replace</span><span> used time:15 </span> </li> <li class="alt"> <span class="func">str_replace</span><span> used time:2 </span> </li> </ol>
str_replace is very fast because it does not require matching, and preg_replace is much faster than ereg_replace.
This concludes the introduction to Ereg regular expressions in PHP. I hope it will be helpful for you to understand and learn Ereg regular expressions in 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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

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
