search
HomeBackend DevelopmentPHP TutorialPOSIX extended regular expression functions

Finding strings is the main application of regular expressions. In PHP, two functions available for matching POSIX-style regular expressions are the ereg() function and the eregi() function.

ereg() function and eregi() function

The function syntax format is as follows:

int ereg( string pattern,string search,array[matches]);

Function function: This function searches the string search and finds a string in pattern that matches the regular expression. If strings are found that match the subexpressions of pattern, these strings will be stored in the matches array, with each array element corresponding to one subexpression.

Function The eregi() function has the same functions as the ereg() function except that it is not case-sensitive.

An example shows using the ereg() function to verify whether the variable is legal:

<?php
header("content-type:text/html;charset=utf-8");
$ereg = &#39;^[$][[:alpha:]__][[:alnum:]]*&#39;;
ereg($ereg,&#39;$_name&#39;,$register);
var_dump($register);
?>

Note: ereg has been deprecated in the new version of PHP and replaced by preg_match() .

ereg_replace() and eregi_replace()

The function syntax format is as follows:

string ereg_replace/ereg_replace(string pattern , string replacement, string string)

Function function: Match the expression pattern in the character replacement string. If the match is successful, use replacement to replace the matching string and return the replaced string. If no match is found in string, string will be returned unchanged. eregi_replace() is not case sensitive.

Example shows how to replace all non-uppercase TMs in the string with uppercase TMs:

<?php
header("content-type:text/html;charset=utf-8");
$ereg = &#39;tm&#39;;
$str = &#39;hello ,tm,Tm,tM&#39;;
$rep_str = eregi_replace($ereg,&#39;TM&#39;,$str);
echo $rep_str;

Note: In the new version, eregi_replace() is replaced by preg_replace().

The above is the detailed content of POSIX extended regular expression functions. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
php怎么去除字符串中的所有大写字母php怎么去除字符串中的所有大写字母Sep 26, 2022 pm 07:59 PM

两种去除方法:1、利用preg_replace()执行正则表达式搜索所有大写字母并将其替换为空字符即可,语法“preg_replace('/[A-Z]/','',$str)”。2、利用preg_filter()执行正则表达式搜索所有大写字母并将其替换为空字符即可,语法“preg_filter('/[A-Z]/','',$str)”。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

使用Go语言编写高性能的正则表达式匹配使用Go语言编写高性能的正则表达式匹配Jun 15, 2023 pm 10:56 PM

随着数据量的不断增大,正则表达式匹配成为了程序中常用的操作之一。而在Go语言中,由于其天然的并行ism,以及与底层系统的交互性和高效性,使得Go语言的正则表达式匹配极具优势。那么如何使用Go语言编写高性能的正则表达式匹配呢?一、了解正则表达式在使用正则表达式前,我们首先需要了解正则表达式,了解其基本语法规则以及常用的匹配字符,使我们能够在编写正则表达式时更加

php怎么利用正则排除字符串中的字符php怎么利用正则排除字符串中的字符Dec 15, 2022 pm 03:30 PM

两种方法:1、用preg_replace(),可执行正则表达式的搜索和替换,只需将字符串中匹配的字符替换为空字符即可,语法“preg_replace(正则, "", $str)”。2、用preg_match_all(),可搜索字符串中所有和正则表达式匹配的结果,会将每次的匹配结果放在一个数组$array中,语法“preg_match_all(正则,$str,$array);”。

php如何使用PHP的POSIX扩展?php如何使用PHP的POSIX扩展?Jun 03, 2023 am 08:01 AM

PHP的POSIX扩展是一组允许PHP与POSIX兼容操作系统进行交互的函数和常量。POSIX(PortableOperatingSystemInterface)是一组操作系统接口标准,旨在允许软件开发人员编写可在各种UNIX或UNIX类操作系统上运行的应用程序。本文将介绍如何使用PHP的POSIX扩展,包括安装和使用。一、安装PHP的POSIX扩展在

php怎么只获取中文字符php怎么只获取中文字符Apr 28, 2022 pm 08:15 PM

php中可用preg_match_all()配合正则表达式过滤字符串,只获取中文字符;语法“preg_match_all("/[\x{4e00}-\x{9fff}]+/u","$str",$arr);”,会将匹配字符存入“$arr”数组中。

javascript怎么正则替换非汉字的字符javascript怎么正则替换非汉字的字符Oct 13, 2022 pm 05:37 PM

在javascript中,可以使用replace()函数配合正则表达式“/[u4e00-u9fa5|,]+/ig”来查找字符串中的所有非汉字字符,并将其替换为其他指定值,语法“字符串对象.replace(/[u4e00-u9fa5|,]+/ig,'指定替换值')”。

Linux and BSD alternative Redox OS reaches version 0.9.0 with COSMIC desktop applications and multiple optimizationsLinux and BSD alternative Redox OS reaches version 0.9.0 with COSMIC desktop applications and multiple optimizationsSep 12, 2024 pm 12:18 PM

On April 20, 2015, Redox OS surfaced as a new microkernel operating system "with a focus on safety, freedom, reliability, correctness, and pragmatism." Written in Rust and assembly language, this project was inspired by pieces of code such

See all articles

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

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version