php正则表达式
<?php
// 常用正则表达式函数总结(都以preg_开头,除了preg_quote之外,第一个参数都是正则表达式)
// 1.preg_match($pattern,$subject,$arr)//匹配单个 表单验证
// 2.preg_match_all($pattern,$subject,$arr)//匹配多个 表单验证
// 3.preg_filter($pattern,$replacement,$subject)只会保留发生替换的字符串
// 4.preg_replace($pattern,$replacement,$subject)会显示所有的字符串 非法词语过滤
// 5.preg_grep($pattern,$subject)只做匹配,不做替换
// 6.preg_split($pattern,$subject)分割字符串作用 跟explode函数作用类似
// 7.preg_quote($str);正则运算转义函数
header('Content-type:text/html;charset="utf-8"');
function show($arr = null)
{
if($arr == null)
{
echo 'null';
}
elseif(is_array($arr))
{
echo '<pre>';
print_r($arr);
echo '</pre>';
}else
{
echo $arr;
}
}
/**
* PHP正则表达式函数
* preg_match($parteen,$str,$arr);匹配单个
* preg_match_all($parteen,$str,$arr);匹配所有
* 返回匹配到的次数
*/
$parteen = '/[0-3]/';
$str = 'weeffdsfsdfd3454fdxvdfg6vxv8';
$replacement = '测试';
/*$m1 = $m2 = array();
$t1 = preg_match($parteen,$str,$m1);
$t2 = preg_match_all($parteen,$str,$m2);
show($m1);
echo '<h1 />';
show($m2);
echo '<h1 />';
echo show($t1.'||'.$t2);*/
/**
* preg_replace 与preg_filter
* preg_repace($pattern,$replacement,$subject)会显示所有的字符串
* preg_filter($pattern,$replacement,$subject)只会保留发生替换的字符串
*/
/*$parteen = array('/[0123]/','/[456]/','[789]');
$str = array('wewe','w234','f454');
$replacement = array('高','矮','胖');
$t1 = preg_replace($parteen,$replacement,$str);
$t2 = preg_filter($parteen,$replacement,$str);
show($t1);
echo '<h1 />';
show($t2);
echo '<h1 />';*/
/**
* preg_grep($pattern,$subject)只做匹配,不做替换
*/
/*$parteen = '/[0-9]/';
$str = array('wewe','w234','f454');
$t1 = preg_grep($parteen,$str);
show($t1);*/
/**
* preg_split($pattern,$subject)分割字符串作用 跟explode函数作用类似
*/
/*$parteen = '/[0-9]/';
$subject = '这4是1一个测试的东西';
$t1 = preg_split($parteen,$subject);
show($t1);*/
/**
* preg_quote($str);正则运算转意
*/
$str = 'qwer[123]{sadas}';
$t1 = preg_quote($str);
show($t1);
?>