Home Backend Development PHP Tutorial Analyzing Ereg regular expressions in PHP_PHP tutorial

Analyzing Ereg regular expressions in PHP_PHP tutorial

Jul 15, 2016 pm 01:26 PM
perl php compatible us regular expression

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>
Copy after login

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>
Copy after login

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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446583.htmlTechArticleWe know that Perl is compatible with regular expressions in PHP, so what do we need to know about Ereg regular expressions? Here we analyze the difference between Perl compatible regular expressions and Perl/Ereg...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles