学习PHP时,你可能会遇到判断PHP正则表达式问题,这里将介绍PHP正则表达式问题的解决方法,在这里拿出来和大家分享一下。在一些查询网站和搜索引擎,常常用到判断某字符串是否为标准的IP地址。
<ol class="dp-xml"> <li class="alt"><span><span>functionis_ip($gonten){ </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">ip</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">explode</font></span><span>(”.”,$gonten); </span> </li> <li class="alt"> <span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$i</span><strong><font color="#006699"><span class="tag"><span class="tag-name">count</span></span></font></strong><span>($ip);$i++) </span> </li> <li class=""><span>{ </span></li> <li class="alt"> <span>if($ip[$i]</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>255){ </span> </li> <li class=""><span>return(0); </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>returnereg(”^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$”,$gonten); </span></li> <li class=""><span>} </span></li> </ol>
$gonten为要查询的字符串,用is_ip($gonten)就可以判断了,返回FALSE和TRUE。
1.eregi语句不区分大小写判断
<ol class="dp-xml"><li class="alt"><span><span>if(eregi("^[a-zA-Z0-9]{8,10}$","999999999"))echo"是八到十位的数字与字母"; </span></span></li></ol>
2.以区分大小写的方式替换文本的ereg_replace
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">string</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"Thisisatest"</font></span><span>; </span></span></li> <li class=""><span>//替换is为was </span></li> <li class="alt"><span>echoereg_replace("is","was",$string); </span></li> </ol>
还可以使用来反引用小括号里的字符0表示全部1表示第一个匹配的字符串以此类推最多9个!具体手册不区分大小写替换文本的eregi_replace用法同上
3.区分大小写的方式划分字符串为不同元素split语句
<ol class="dp-xml"><li class="alt"><span><span>print_r(split("[nt]","我爱n北京t天安门")); </span></span></li></ol>
不区分大小写的划分:spliti语句用法同上
4.调节只支持区分大小写的正则表达式sql_regcase
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute">name</span><span>=</span><span class="attribute-value">"xuandun"</span><span>; </span></span></li> <li class=""><span>echosql_regcase($name); </span></li> </ol>