设为首页收藏本站language 语言切换
查看: 1401|回复: 0
收起左侧

PHP正则表达式函数

[复制链接]
发表于 2010-2-24 13:39:01 | 显示全部楼层 |阅读模式
<SPAN class=x14><>关于正则表达式 (Regular expression),似乎译成:字符串解析处理,较合乎字面上的意义。但目前所有的相关书籍都译成正则表达式或是正规表示法....等等,实际上是负责字符串解析比对,并对字符串做相关的处理。 </P><>ereg: 字符串比对解析。 <BR>ereg_replace: 字符串比对解析并取代。 <BR>eregi: 字符串比对解析,与大小写无关。 <BR>eregi_replace: 字符串比对解析并取代,与大小写无关。 <BR>split: 将字符串依指定的规则切开。 <BR>sql_regcase: 将字符串逐字返回大小写字符。</P><H1><FONT color=#0000bb>ereg</FONT></H1><>字符串比对解析。 </P><><FONT color=#ff8000>语法:</FONT><STRONG>int ereg(string pattern, string string, array [regs]);</STRONG></P><><FONT color=#ff8000>返回值:</FONT> 整数/数组 </P><><FONT color=#ff8000>函数种类:</FONT> 资料处理 </P><> </P><><TABLE height=1 cellSpacing=0 cellPadding=0 width="100%" border=0><TBODY><TR><TD bgColor=#c0c0c0 height=1> </TD></TR><TR><TD align=left><FONT color=#ff8000>内容说明</FONT></TD></TR></TBODY></TABLE></P><>本函数以 pattern 的规则来解析比对字符串 string。比对结果返回的值放在数组参数 regs 之中,regs[0] 内容就是原字符串 string、regs[1] 为第一个合乎规则的字符串、regs[2] 就是第二个合乎规则的字符串,余类推。若省略参数 regs,则只是单纯地比对,找到则返回值为 true。 </P><> </P><><TABLE height=1 cellSpacing=0 cellPadding=0 width="100%" border=0><TBODY><TR><TD bgColor=#c0c0c0 height=1> </TD></TR><TR><TD align=left><FONT color=#ff8000>使用范例</FONT></TD></TR></TBODY></TABLE></P><><FONT color=#000000>这个例子是 markus@dnet.it 在 14-Jun-1999 所提出的,可对使用者输入的 E-Mail 作简单的检查,检查使用者的 E-Mail 字符串是否有 @ 字符,在 @ 字符前有英文字母或数字,在之后有数节字符串,最后的小数点后只能有二个或三个英文字母。super@mail.wilson.gs 就可以通过检查,super@mail.wilson 就不能通过检查。<BR><BR><FONT color=#0000bb>&lt;?php<BR></FONT><FONT color=#007700>if (</FONT><FONT color=#0000bb>eregi</FONT><FONT color=#007700>(</FONT><FONT color=#dd0000>"^</FONT><FONT color=#007700>[</FONT><FONT color=#dd0000>_\.0-9a-z-</FONT><FONT color=#007700>]</FONT><FONT color=#dd0000>+@(</FONT><FONT color=#007700>[</FONT><FONT color=#dd0000>0-9a-z</FONT><FONT color=#007700>][</FONT><FONT color=#dd0000>0-9a-z-</FONT><FONT color=#007700>]</FONT><FONT color=#dd0000>+\.)+</FONT><FONT color=#007700>[</FONT><FONT color=#dd0000>a-z</FONT><FONT color=#007700>]{</FONT><FONT color=#dd0000>2,3</FONT><FONT color=#007700>}$</FONT><FONT color=#dd0000>"</FONT><FONT color=#007700>,</FONT><FONT color=#0000bb>$email</FONT><FONT color=#007700>)) {<BR>  echo </FONT><FONT color=#dd0000>"您的 E-Mail 通过初步检查"</FONT><FONT color=#007700>;<BR>}<BR></FONT><FONT color=#0000bb>?&gt;</FONT> <BR><BR><SPAN class=x14></P><></P><H1><FONT color=#0000bb>ereg_replace</FONT></H1><>字符串比对解析并取代。 </P><><FONT color=#ff8000>语法:</FONT><STRONG>string ereg_replace(string pattern, string replacement, string string);</STRONG></P><><FONT color=#ff8000>返回值:</FONT> 字符串 </P><><FONT color=#ff8000>函数种类:</FONT> 资料处理 </P><> </P><><TABLE height=1 cellSpacing=0 cellPadding=0 width="100%" border=0><TBODY><TR><TD bgColor=#c0c0c0 height=1> </TD></TR><TR><TD align=left><FONT color=#ff8000>内容说明</FONT></TD></TR></TBODY></TABLE></P><>本函数以 pattern 的规则来解析比对字符串 string,欲取而代之的字符串为参数 replacement。返回值为字符串类型,为取代后的字符串结果。 </P><> </P><><TABLE height=1 cellSpacing=0 cellPadding=0 width="100%" border=0><TBODY><TR><TD bgColor=#c0c0c0 height=1> </TD></TR><TR><TD align=left><FONT color=#ff8000>使用范例</FONT></TD></TR></TBODY></TABLE></P><><FONT color=#000000>ken@freebsdrocks.com 在 16-Mar-1999 提出的例子。<BR><BR><FONT color=#0000bb>&lt;?php<BR>$text </FONT><FONT color=#007700>= </FONT><FONT color=#dd0000>'This is a {1} day, not {2} and {3}.'</FONT><FONT color=#007700>;<BR></FONT><FONT color=#0000bb>$daytype </FONT><FONT color=#007700>= array( </FONT><FONT color=#0000bb>1 </FONT><FONT color=#007700>=&gt; </FONT><FONT color=#dd0000>'fine'</FONT><FONT color=#007700>,<BR>                  </FONT><FONT color=#0000bb>2 </FONT><FONT color=#007700>=&gt; </FONT><FONT color=#dd0000>'overcast'</FONT><FONT color=#007700>,<BR>                  </FONT><FONT color=#0000bb>3 </FONT><FONT color=#007700>=&gt; </FONT><FONT color=#dd0000>'rainy' </FONT><FONT color=#007700>);<BR>while (</FONT><FONT color=#0000bb>ereg </FONT><FONT color=#007700>(</FONT><FONT color=#dd0000>'{([0-9]+)}'</FONT><FONT color=#007700>, </FONT><FONT color=#0000bb>$text</FONT><FONT color=#007700>, </FONT><FONT color=#0000bb>$regs</FONT><FONT color=#007700>)) {<BR>  </FONT><FONT color=#0000bb>$found </FONT><FONT color=#007700>= </FONT><FONT color=#0000bb>$regs</FONT><FONT color=#007700>[</FONT><FONT color=#0000bb>1</FONT><FONT color=#007700>];<BR>  </FONT><FONT color=#0000bb>$text </FONT><FONT color=#007700>= </FONT><FONT color=#0000bb>ereg_replace</FONT><FONT color=#007700>(</FONT><FONT color=#dd0000>"\{"</FONT><FONT color=#007700>.</FONT><FONT color=#0000bb>$found</FONT><FONT color=#007700>.</FONT><FONT color=#dd0000>"\}"</FONT><FONT color=#007700>, </FONT><FONT color=#0000bb>$daytype</FONT><FONT color=#007700>[</FONT><FONT color=#0000bb>$found</FONT><FONT color=#007700>], </FONT><FONT color=#0000bb>$text</FONT><FONT color=#007700>);<BR>}<BR>echo </FONT><FONT color=#dd0000>"$text</FONT><FONT color=#007700>\n</FONT><FONT color=#dd0000>"</FONT><FONT color=#007700>;<BR></FONT><FONT color=#ff8000>// This is a fine day, not overcast and rainy. <BR></FONT><FONT color=#0000bb>?&gt;<BR></FONT><BR>ken@freebsdrocks.com 并同时提出具有相同功能的perl 程序范例如下:<BR><BR>$text = 'This is a {1} day, not {2} and {3}.';<BR>%daytype = ( 1 =&gt; 'fine',<BR>             2 =&gt; 'overcast',<BR>             3 =&gt; 'rainy' );<BR>$text =~ s/{(\d+)}/$daytype{$1}/eg;<BR>print "$text\n";</FONT></P><>结果是:'This is a fine day, not overcast and rainy.<BR><BR><SPAN class=x14> </P><></P><H1><FONT color=#0000bb>eregi</FONT></H1><>字符串比对解析,与大小写无关。 </P><><FONT color=#ff8000>语法:</FONT><STRONG>int eregi(string pattern, string string, array [regs]);</STRONG></P><><FONT color=#ff8000>返回值:</FONT> 整数/数组 </P><><FONT color=#ff8000>函数种类:</FONT> 资料处理 </P><> </P><><TABLE height=1 cellSpacing=0 cellPadding=0 width="100%" border=0><TBODY><TR><TD bgColor=#c0c0c0 height=1> </TD></TR><TR><TD align=left><FONT color=#ff8000>内容说明</FONT></TD></TR></TBODY></TABLE></P><>本函数和 <FONT color=#6666ff>ereg()</FONT> 类似,用法也相同。不同之处在于 <FONT color=#6666ff>ereg()</FONT> 有区分大小写,本函数与大小写无关。</P><H1><FONT color=#0000bb>eregi_replace</FONT></H1><>字符串比对解析并取代,与大小写无关。 </P><><FONT color=#ff8000>语法:</FONT><STRONG>string eregi_replace(string pattern, string replacement, string string);</STRONG></P><><FONT color=#ff8000>返回值:</FONT> 字符串 </P><><FONT color=#ff8000>函数种类:</FONT> 资料处理 </P><> </P><><TABLE height=1 cellSpacing=0 cellPadding=0 width="100%" border=0><TBODY><TR><TD bgColor=#c0c0c0 height=1> </TD></TR><TR><TD align=left><FONT color=#ff8000>内容说明</FONT></TD></TR></TBODY></TABLE></P><>本函数和 <FONT color=#6666ff>ereg_replace()</FONT> 类似,用法也相同。不同之处在于 <FONT color=#6666ff>ereg_replace()</FONT> 有区分大小写,本函数与大小写无关。<BR><BR><SPAN class=x14> </P><></P><H1><FONT color=#0000bb>split</FONT></H1><>将字符串依指定的规则切开。 </P><><FONT color=#ff8000>语法:</FONT><STRONG>array split(string pattern, string string, int [limit]);</STRONG></P><><FONT color=#ff8000>返回值:</FONT> 数组 </P><><FONT color=#ff8000>函数种类:</FONT> 资料处理 </P><> </P><><TABLE height=1 cellSpacing=0 cellPadding=0 width="100%" border=0><TBODY><TR><TD bgColor=#c0c0c0 height=1> </TD></TR><TR><TD align=left><FONT color=#ff8000>内容说明</FONT></TD></TR></TBODY></TABLE></P><>本函数可将字符串依指定的规则分开。切开后的返回值为数组变量。参数 pattern 为指定的规则字符串、参数 string 则为待处理的字符串、参数 limit 可省略,表示欲处理的最多合乎值。值得注意的是本函数的 pattern 参数有区分大小写。</P><H1><FONT color=#0000bb>sql_regcase</FONT></H1><>将字符串逐字返回大小写字符。 </P><><FONT color=#ff8000>语法:</FONT><STRONG>string sql_regcase(string string);</STRONG></P><><FONT color=#ff8000>返回值:</FONT> 数组 </P><><FONT color=#ff8000>函数种类:</FONT> 资料处理 </P><> </P><><TABLE height=1 cellSpacing=0 cellPadding=0 width="100%" border=0><TBODY><TR><TD bgColor=#c0c0c0 height=1> </TD></TR><TR><TD align=left><FONT color=#ff8000>内容说明</FONT></TD></TR></TBODY></TABLE></P><>本函数可将字符串之字符逐字返回大小写。在 PHP 使用上,本函数没有什么作用,但可能可以提供外部程序或数据库处理。 </P><> </P><><TABLE height=1 cellSpacing=0 cellPadding=0 width="100%" border=0><TBODY><TR><TD bgColor=#c0c0c0 height=1> </TD></TR><TR><TD align=left><FONT color=#ff8000>使用范例</FONT></TD></TR></TBODY></TABLE></P><><FONT color=#000000>例中的返回字符串为 [Ww][Ii][Ll][Ss][Oo][Nn]<BR><BR><FONT color=#0000bb>&lt;?php<BR></FONT><FONT color=#007700>print(</FONT><FONT color=#0000bb>sql_regcase</FONT><FONT color=#007700>(</FONT><FONT color=#dd0000>"wilson"</FONT><FONT color=#007700>));<BR></FONT><FONT color=#0000bb>?&gt;</FONT></FONT></P></SPAN></SPAN></SPAN></FONT></SPAN>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-8 23:42 , Processed in 0.206649 second(s), 23 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表