php正则表达式函数都有那些呢?那么php正则表达式函数具体的是代表什么呢?具体的用法又是什么呢?让我们一一向你介绍。php正则表达式主要用于复杂字符串的处理。其主要的php正则表达式函数如下:
◆ereg()
◆ereg_replace()
◆eregi()
◆eregi_replace()
立即学习“PHP免费学习笔记(深入)”;
◆split()
PHP正则表达式函数用法归纳:
(1)ereg,eregi
这是正规表达式匹配函数,前者是大小写有关匹配,后者则是无关的.
用法:
10分钟内自己学会PHP其中,第1篇为入门篇,主要包括了解PHP、PHP开发环境搭建、PHP开发基础、PHP流程控制语句、函数、字符串操作、正则表达式、PHP数组、PHP与Web页面交互、日期和时间等内容;第2篇为提高篇,主要包括MySQL数据库设计、PHP操作MySQL数据库、Cookie和Session、图形图像处理技术、文件和目录处理技术、面向对象、PDO数据库抽象层、程序调试与错误处理、A
524
ereg(正规表达式,字符串,[匹配部分数组名]); PHP3.0中的正规表达式大体类似于grep中用的.
(2)ereg_replace,eregi_replace
这些是替换函数.
用法:
ereg_replace(正规表达式,替换串,原字符串);
字符串处理函数中有一个strtr,是"翻译"函数,类似于Perl中的tr/.../.../,
用法:
strtr(字符串,"从","到");
例如:
strtr("aaabb","ab","cd")返回"cccdd".
(3)split
与explode函数有些类似,但这次可以在匹配某正规表达式的地方分割字符串.
用法:
split(正规表达式,字符串,[取出前多少项]);
这些函数都使用正则字符串做为第一个参数。PHP使用Posix 1003.2标准所定义的扩展正则字符串。要查考Posix正则表达式的完整描述请看PHP软件包中regex目录下的man页。
PHP正则表达式函数应用实例:
<OL class=dp-c><LI class=alt><SPAN><SPAN class=func>ereg</SPAN><SPAN>(</SPAN><SPAN class=string>"abc"</SPAN><SPAN>,</SPAN><SPAN class=vars>$string</SPAN><SPAN>); </SPAN></SPAN><LI><SPAN class=comment>/* Returns true if "abc" is </SPAN> <LI class=alt><SPAN><SPAN class=comment>found anywhere in $string. */</SPAN><SPAN> </SPAN></SPAN><LI><SPAN> </SPAN><LI class=alt><SPAN class=func>ereg</SPAN><SPAN>(</SPAN><SPAN class=string>"^abc"</SPAN><SPAN>,</SPAN><SPAN class=vars>$string</SPAN><SPAN>); </SPAN><LI><SPAN class=comment>/* Returns true if "abc" is </SPAN> <LI class=alt><SPAN><SPAN class=comment>found at the beginning of $string. */</SPAN><SPAN> </SPAN></SPAN><LI><SPAN> </SPAN><LI class=alt><SPAN class=func>ereg</SPAN><SPAN>(</SPAN><SPAN class=string>"abc$"</SPAN><SPAN>,</SPAN><SPAN class=vars>$string</SPAN><SPAN>); </SPAN><LI><SPAN class=comment>/* Returns true if "abc" is </SPAN> <LI class=alt><SPAN><SPAN class=comment> found at the end of $string. */</SPAN><SPAN> </SPAN></SPAN><LI><SPAN> </SPAN><LI class=alt><SPAN class=func>eregi</SPAN><SPAN>(</SPAN><SPAN class=string>"(ozilla.[23]|MSIE.3)"</SPAN><SPAN>,</SPAN><SPAN class=vars>$HTTP_USER_AGENT</SPAN><SPAN>); </SPAN><LI><SPAN class=comment>/* Returns true if client browser </SPAN> <LI class=alt><SPAN><SPAN class=comment> is Netscape 2, 3 or MSIE 3. */</SPAN><SPAN> </SPAN></SPAN><LI><SPAN> </SPAN><LI class=alt><SPAN class=func>ereg</SPAN><SPAN>("([[:alnum:]]+) ([[:alnum:]]+) </SPAN><LI><SPAN> ([[:alnum:]]+)",</SPAN><SPAN class=vars>$string</SPAN><SPAN>,</SPAN><SPAN class=vars>$regs</SPAN><SPAN>); </SPAN><LI class=alt><SPAN class=comment>/* Places three space separated words </SPAN> <LI><SPAN><SPAN class=comment>into $regs[1], $regs[2] and $regs[3]. */</SPAN><SPAN> </SPAN></SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=func>ereg_replace</SPAN><SPAN>(</SPAN><SPAN class=string>"^"</SPAN><SPAN>,</SPAN><SPAN class=string>""</SPAN><SPAN>,</SPAN><SPAN class=vars>$string</SPAN><SPAN>); </SPAN><LI class=alt><SPAN class=comment>/* Put a tag at the beginning of $string. */</SPAN><SPAN> </SPAN><LI><SPAN> </SPAN><LI class=alt><SPAN class=func>ereg_replace</SPAN><SPAN>(</SPAN><SPAN class=string>"$"</SPAN><SPAN>,</SPAN><SPAN class=string>""</SPAN><SPAN>,</SPAN><SPAN class=vars>$string</SPAN><SPAN>); </SPAN><LI><SPAN class=comment>/* Put a tag at the end of $string. */</SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=func>ereg_replace</SPAN><SPAN>(</SPAN><SPAN class=string>" "</SPAN><SPAN>,</SPAN><SPAN class=string>""</SPAN><SPAN>,</SPAN><SPAN class=vars>$string</SPAN><SPAN>); </SPAN><LI class=alt><SPAN class=comment>/* Get rid of any carriage </SPAN> <LI><SPAN><SPAN class=comment>return characters in $string. */</SPAN><SPAN> </SPAN></SPAN></LI></OL>PHP正则表达式函数的相关内容就向你介绍到这里,希望对你了解和学习PHP正则表达式函数有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号