在php中正则表达式用于复杂字符串的处理。所支持的正则表达式如下:
ereg()
ereg_replace()
eregi()
eregi_replace()
split()
(1)ereg,eregi
这是正规表达式匹配函数,前者是大小写有关匹配,后者则是无关的.
用法:
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页。
regular expression examples:
ereg("abc",$string);
/* returns true if "abc" is found anywhere in $string. */
ereg("^abc",$string);
/* returns true if "abc" is found at the beginning of $string. */
ereg("abc$",$string);
/* returns true if "abc" is found at the end of $string. */
eregi("(ozilla.[23]msie.3)",$http_user_agent);
/* returns true if client browser is.netscape 2, 3 or msie 3. */
ereg("([[:alnum:]]+) ([[:alnum:]]+) ([[:alnum:]]+)",$string,$regs);
/* places three space separated words into $regs[1], $regs[2] and $regs[3]. */
ereg_replace("^","",$string);
/* put a tag at the beginning of $string. */
ereg_replace("$","",$string);
/* put a tag at the end of $string. */
ereg_replace(" ","",$string);
/* get rid of any carriage return characters in $string. */
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器