[PHP函数]PHP正则表达式函数

作者:笨小孩 发布于:2012-10-18 15:49 分类:PHP

<pre>
<?php
/*PHP正则表达式函数
*2009.12.07
*/
//Perl—————————————————————————————–
//1.preg_grep()
//匹配所有仅由有一个单词组成的科目名
echo ‘<b>preg_grep()</b><br />’;
$pattern = “/^[a-z]*$/i”;
$subjects = array(“Mechanical Engineering”, “Medicine”,”Social Science”, “Agriculture”, “Commercial Science”, “Politics” );
$alonewords = preg_grep($pattern, $subjects);
var_dump(array_values($alonewords));//重新索引
echo ‘————————————-<br />’;

//2.preg_match()
//匹配时间
echo ‘<b>preg_match()</b><br />’;
$pattern = “/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}) ([ap]m)/”; //括号的作用,规划数组
$content = “Current date and time is “.date(“Y-m-d h:i a”).”, we are learning PHP together.”;
if (preg_match ($pattern, $content, $m)) {
var_dump($m);
}else{
echo ‘未匹配到’;
}
echo ‘————————————-<br />’;

//3.preg_match_all()
echo ‘<b>preg_match_all()</b><br />’;
//注意与preg_match()的区别
preg_match_all(“/{(\w)+}/”,”{x}{y}{z}”,$abc); //从头到尾全部匹配
var_dump($abc);
preg_match(“/{(\w)+}/”,”{x}{y}{z}”,$abc2); //找到一个就停
var_dump($abc2);
echo ‘————————————-<br />’;

//4.preg_replace()
//正则表达式的替换
echo ‘<b>preg_replace()</b><br />’;
$string = “Name: {Name}\nEmail: {Email}\nAddress: {Address}\n”;
//模式
$patterns =array(“/{Address}/”,”/{Name}/”,”/{Email}/”);
//替换字串
$replacements = array (“No.5, Wilson St., New York, U.S.A”,”Thomas Ching”,”tom@emailaddress.com”,);
print preg_replace($patterns, $replacements, $string);
echo ‘<br />’;
//还可以使用模式修正符“e”。
$html_body = “&ZHANG&@HAHA@HEHE”;
//输出结果中HTML标签将全部为小写字母
echo preg_replace (“/(&\w+&)(@\w+@)(\w+)/e”, “‘\\1′.strtolower(‘\\2′).’\\3′”,$html_body);//此处的模式变量\\2将被strtolower转换为小写字符
echo ‘<br />————————————-<br />’;

//5.preg_split()
//preg_split()函数使用了Perl兼容正则表达式语法,通常是比split()更快的替代方案。使用正则表达式的方法分割字符串,可以使用 更广泛的分隔字符。如果仅用某个特定的字符进行分割,建议使用explode()函数,它不调用正则表达式引擎,因此速度是最快的。
echo ‘<b>preg_split()</b><br />’;
$seek = array();
$text = “I have a dream that one day I can make it. So just do it, nothing is impossible!”;
//将字符串按空白,标点符号拆分(每个标点后也可能跟有空格)
$words = preg_split(“/[.,;!\s']\s*/”, $text);
foreach($words as $val) {
$seek[strtolower($val)] ++;
}
var_dump($seek);
echo “共有大约” .count($words). “个单词。”;
echo “其中共有” .$seek['i']. “个单词“I”。”;
echo ‘<br />————————————-<br />’;

//6.preg_quote()
//为参数并给其中每个属于正则表达式语法的字符前面加上一个反斜线。正则表达式的特殊字符包括:. \ + * ? [ ^ ] $ ( ) { } = ! < > | :。
echo ‘<b>preg_quote()</b><br />’;
$keywords = ‘$40 for a g3/400′;
$keywords = preg_quote($keywords, ‘/’);
echo $keywords;
echo ‘<br />————————————-<br />’;

//POSIX————————————————————————————
//1.ereg()、eregi()
//通常情况下,使用与Perl兼容的正则表达式匹配函数perg_match(),将比使用ereg()或eregi()的速度更快。如果只是查找一个字符串中是否包含某个子字符串,建议使用strstr()或strpos()函数。
//检测邮箱格式
echo ‘<b>eregi()</b><br />’;
$email = “xxx@163.com”;
$pattern = ‘^[a-zA-Z0-9\-]{5,20}@[a-zA-Z0-9]{1,10}\.[a-zA-Z]{2,4}$’;//正则表达式字符串不需要使用”/”作为开始和结束的标志
$result = eregi($pattern,$email);
var_dump($result);
echo ‘————————————-<br />’;

//2.ereg_replace()、eregi_replace()
echo ‘<b>ereg_replace()、eregi_replace()</b><br />’;
$lines = ‘zhangjian-13-t18′;
echo eregi_replace(“[\-]“, “*”, $lines);
echo ‘<br />————————————-<br />’;

//3.split()、spliti()
//正则表达式的拆分
echo ‘<b>split()、spliti() </b><br />’;
$date = date(“Y-m-d”);
//分隔符可以是斜线,点,或横线
list($month, $day, $year) = split (‘[/.-]‘, $date);
//输出为另一种时间格式
echo “Month: $month; Day: $day; Year: $year<br />\n”;
?>

标签: PHP函数

发表评论:

手机版 | 
Copyright © 2012 lamp46.com All Right Reserved. sitemap