1 获取文件扩展名.
2 验证字符串中是否有内容
3关于正则的学习
3.1 得到数组中满足要求的单元 preg_grep
3.2 验证字符串中是否有指定字符。 高级玩法,preg_match_all 将结果置入数组。
3.2 将其中的特殊字符转义。
3.3 正则替换使用\\n
3.4 正则替换+代理。
3.5 英文分词.
3.6 取前十位,后面变…
4 常用正则示例
1 获取文件扩展名.
<?php
//方法一:
function extend_1($file_name)
{
$retval="";
$pt=strrpos($file_name, ".");
if ($pt) $retval=substr($file_name, $pt+1, strlen($file_name) - $pt);
return ($retval);
}
//方法二
function extend_2($file_name)
{
$extend = pathinfo($file_name);
$extend = strtolower($extend["extension"]);
return $extend;
}
//方法三
function extend_3($file_name)
{
$extend =explode("." , $file_name);
$va=count($extend)-1;
return $extend[$va];
}
?>
2 验证字符串中是否有内容
if(ereg("www",$str1)) 有则true 无则 flase;
3关于正则的学习
3.1得到数组中满足要求的单元
<?php
$array = array(0=>"111",1=>"2243W2",2=>"333",3=>"aaa");
$fl_array = array_values(preg_grep ("/^(\d+)$/", $array));
print_r ($fl_array) ;
?>
3.2验证字符串中是否有指定字符。
<?
初级玩法.
if( preg_match("/www/","we are the chfwww"))
echo "纯在";
echo strpos("fsdf awwwthefdsafd wwwchf","www",1) ;
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a');
echo $pos;
$email = 'user@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
高级玩法。preg_match (pattern,str,match)数组的第0位是全匹配,第一位是满足()里面的东西。
$contant="aaa<img src='../images/bannersms.jpg' fdsafds>aaa";
preg_match_all("/<img.+src=('|\")(.+)\\1/isU",$contant,$a); 注意这里的很经典的\\1 是将第一个()中的东西用来配对.
print_r( $a ); 对数组来解析 $a[0] 为<img src='../images/bannersms.jpg' fdsafds>
()中的内容将后被缓存到数组中去。
3.2 将其中的特殊字符转义。
$textbody = "This book is *very* difficult to find.";
$word = "*very*";
$textbody = preg_replace ("/".preg_quote($word)."/",
"<B>".$word."</B>",
$textbody);
print($textbody);
3.3 正则替换使用\\n
$string = "April555# 15, 2003";
$pattern = "/(\w+#) (\d+), (\d+)/i";
$replacement = "\$3 \${1}";
print preg_replace($pattern, $replacement, $string);
3.4正则替换+代理
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
function next_year($matches) {
return $matches[1].($matches[2]+1);
}
echo preg_replace_callback("|(\d{2}/\d{2}/)(\d{4})|", "next_year", $text);
3.5 英文分词.
$keywords = preg_split ("/[\s,]+/", "hypertext language, programming",-1,PREG_SPLIT_OFFSET_CAPTURE);
print_r ($keywords);
3.6 取前十位,后面变…
$str = "432.,,.af我fdsa人有的和主产不为这工要在地";
$text = preg_replace("/([.\.]{10})(.)*/","\$1...",$str);
echo $text;
?>
常用正则4 常用正则示例
|
|
作者:tot 提供 时间: 2004-11-04 文档类型:原创 来自:蓝色理想 浏览统计 total:58218 | year:963 | Quarter:963 | Month:963 | Week:205 | today:95
|
|
|
匹配中文字符的正则表达式: [\u4e00-\u9fa5] 匹配双字节字符(包括汉字在内):[^\x00-\xff]
应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}
匹配空行的正则表达式:\n[\s| ]*\r
匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/
匹配首尾空格的正则表达式:(^\s*)|(\s*$)
应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:
String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }
利用正则表达式分解和转换IP地址:
下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的Javascript程序:
function IP2V(ip) { re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正则表达式 if(re.test(ip)) { return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1 } else { throw new Error("Not a valid IP address!") } }
不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下:
var ip="10.100.20.168" ip=ip.split(".") alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
匹配网址URL的正则表达式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
利用正则表达式去除字串中重复的字符的算法程序:[注:此程序不正确,原因见本贴回复]
var s="abacabefgeeii" var s1=s.replace(/(.).*\1/g,"$1") var re=new RegExp("["+s1+"]","g") var s2=s.replace(re,"") alert(s1+s2) //结果为:abcefgi
我原来在CSDN上发贴寻求一个表达式来实现去除重复字符的方法,最终没有找到,这是我能想到的最简单的实现方法。思路是使用后向引用取出包括重复的字符,再以重复的字符建立第二个表达式,取到不重复的字符,两者串连。这个方法对于字符顺序有要求的字符串可能不适用。
得用正则表达式从URL地址中提取文件名的javascript程序,如下结果为page1
s="http://www.9499.net/page1.htm" s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2") alert(s)
利用正则表达式限制网页表单里的文本框输入内容:
用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"
用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"
用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
|