php正则表达式函数- 匹配\替换\分割
//------------------匹配函数;
preg_match($mode,$str,$content);
//$mode是正则匹配的模式:
//$str是要与模式匹配的字符串;
//$content是一个"数组";匹配成功的字符串会放入里面;
preg_grep(模式,目标数组); //返回与模式匹配的字符串,返回数组;
例://获取数组中没有空格的字符串;
$arr=array("aaa b2yy","sdw","hello wo5rld","ddd","erfef sfd");
$arr2=preg_grep("/^[a-z]*$/i",$arr);
print_r($arr2);//输出:Array ( [1] => sdw [3] => ddd ) ;
//如果查找数组里字符串中包含数字的字符串,模式为:"/\d/";
//符合模式就会取出来而不会只返回匹配的数字;
//如上输出:Array ( [1] => aaa b2yy [3] => hello wo5rld ) ;
preg_match_all(模式,目标数组,返回数组,[int]); //进行全局正则表达式匹配;匹配所有符合模式的;
例://把全文中所有网址全部变成连接的形式;
$strurl="phpchina网站:http//:www.phpchina.com,php全球网站:http://www.php.net,php中文网站:http://www.php.cn";
echo str2url($strurl);
function str2url($strurl)
{
preg_match_all("/http:\/\/(www\.)?.+\.(com|cn|net)$/i",$strurl,$urls);
print_r($urls);
for($i=0;$i<=count($urls[0]);$i++)
{
return "<a href={$urls[0][$i]}>{$urls[0][$i]}</a>";
}
}
//------------------替换函数;
preg_replace(); //执行正则表达式的搜索和替换;
例:
str_replace()函数的替换:
//string1:被替换部分字符串;
//string2:要替换成的字符串;
//$str:替换后的字符串;
//[int]:限制长度,可选;
$str1="this is a demo!";
echo str_replace("is","MM",$str1);
//把字符串里面的is替换成MM,
//输出:thMM MM a demo!;
//其不能把如下字符串中所有数字一次性替换;
$str="th3is i4s a de5mo!"
//下面使用正则替换函数替换如上字符串;
preg_replace(string1,string2,$str,[int]);
$str2="th3is i4s a de5mo!"
//现在替换里面的所有数字成GG;
echo preg_replace("/\d/","GG",$str2);
//输出:thGGis iGGs a deGGmo!
//如果使用最后一个可选参数限制下;
//让他只替换第一个数字;
echo preg_replace("/\d/","GG",$str2,1);
//输出:thGGis i4s a de5mo!
//------------------分割函数;
preg_split(分割模式,被分割字符串,);
//分割模式,如分割一篇文章用标点分割
//可用这样的模式,如:"/[, . ? ! \s]/";
preg_match($mode,$str,$content);
//$mode是正则匹配的模式:
//$str是要与模式匹配的字符串;
//$content是一个"数组";匹配成功的字符串会放入里面;
preg_grep(模式,目标数组); //返回与模式匹配的字符串,返回数组;
例://获取数组中没有空格的字符串;
$arr=array("aaa b2yy","sdw","hello wo5rld","ddd","erfef sfd");
$arr2=preg_grep("/^[a-z]*$/i",$arr);
print_r($arr2);//输出:Array ( [1] => sdw [3] => ddd ) ;
//如果查找数组里字符串中包含数字的字符串,模式为:"/\d/";
//符合模式就会取出来而不会只返回匹配的数字;
//如上输出:Array ( [1] => aaa b2yy [3] => hello wo5rld ) ;
preg_match_all(模式,目标数组,返回数组,[int]); //进行全局正则表达式匹配;匹配所有符合模式的;
例://把全文中所有网址全部变成连接的形式;
$strurl="phpchina网站:http//:www.phpchina.com,php全球网站:http://www.php.net,php中文网站:http://www.php.cn";
echo str2url($strurl);
function str2url($strurl)
{
preg_match_all("/http:\/\/(www\.)?.+\.(com|cn|net)$/i",$strurl,$urls);
print_r($urls);
for($i=0;$i<=count($urls[0]);$i++)
{
return "<a href={$urls[0][$i]}>{$urls[0][$i]}</a>";
}
}
//------------------替换函数;
preg_replace(); //执行正则表达式的搜索和替换;
例:
str_replace()函数的替换:
//string1:被替换部分字符串;
//string2:要替换成的字符串;
//$str:替换后的字符串;
//[int]:限制长度,可选;
$str1="this is a demo!";
echo str_replace("is","MM",$str1);
//把字符串里面的is替换成MM,
//输出:thMM MM a demo!;
//其不能把如下字符串中所有数字一次性替换;
$str="th3is i4s a de5mo!"
//下面使用正则替换函数替换如上字符串;
preg_replace(string1,string2,$str,[int]);
$str2="th3is i4s a de5mo!"
//现在替换里面的所有数字成GG;
echo preg_replace("/\d/","GG",$str2);
//输出:thGGis iGGs a deGGmo!
//如果使用最后一个可选参数限制下;
//让他只替换第一个数字;
echo preg_replace("/\d/","GG",$str2,1);
//输出:thGGis i4s a de5mo!
//------------------分割函数;
preg_split(分割模式,被分割字符串,);
//分割模式,如分割一篇文章用标点分割
//可用这样的模式,如:"/[, . ? ! \s]/";
下一篇:php正则表达式模式修正符