我的php笔记四:正则表达式
正则表达式直接把东西亮明的感觉,挺好 /cy
基本语法: int ereg ( string pattern, string string [, array ®s] )
常用:ereg, ereg_replace, eregi_replace, eregi, split, spliti
php手册中注解说 使用 Perl 兼容正则表达式语法的 preg_match() 函数通常是比 ereg() 更快的替代方案 还没研究呢
替换网址为链接:
<?php
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
"<a href=\"\\0\">\\0</a>", $text);
?>
/*如果 pattern 包含有括号内的子串,则 replacement 可以包含形如 \\digit 的子串,这些子串将被替换为数字表示的的第几个括号内的子串;\\0 则包含了字符串的整个内容。最多可以用九个子串。括号可以嵌套,此情形下以左圆括号来计算顺序*/
偶的练习 通过正则判断替换邮箱地址:
function check_mail($mail){
ereg("([a-zA-Z0-9].+)(@[a-zA-Z0-9].+)(\.[a-zA-Z]+$)",$mail,$mails);
if (strlen($mails[1])>0 && strlen($mails[2])>0 && strlen($mails[3])>0) {
$newmail= ereg_replace($mails[2],”@yahoo”,$mail);
echo $newmail;
}
else
echo “邮箱地址错误,你忽悠我”;
}
echo check_mail(”asdfsd@sdfsd.com”).”
“;
echo check_mail(”sdfasdf.com
“).”
“;
echo check_mail(”asdfsd.comsdfas@asdf.com”).”
“;
?>















Heartshare » Blog Archive » PHP學習的一些站點和資源搜集
[...] http://san-tin.com/blog/2006/12/31/161903/ http://www.zhaomingliang.com/category/php/ 很好呵 http://www.bssn.org/bssn-php-erge.html http://www.elaguan.net/show-290-1.html http://www.webxuexi.net/catalog.asp?cate=7 [...]