常用的正则表达式去重、匹配img标签、13位时间戳

前端之家收集整理的这篇文章主要介绍了常用的正则表达式去重、匹配img标签、13位时间戳前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
$content : 当前的文章
$order :全部匹配


function parseImgs($content,$order='all')
{ 
    $pattern='/<img.*?src="(.*?)".*?>/is';  
    preg_match_all($pattern,$content,$match); 
    if(isset($match[1])&&!empty($match[1])){
        if($order==='all'){
            return [
            	'exists' => 'y',//图片是否存在 
		        'url' => $match[1]    //存在对应的URL
            ];//$match[1];
        }
        if(is_numeric($order)&&isset($match[1][$order])){
            return [
            	'exists' => 'y',//图片是否存在 
		        'url' => $match[1][$order]    //存在对应的URL
            ];//return $match[1][$order];
        }
    }
    return [
    	'exists' => 'n',//图片是否存在 
		 'url' => []    //存在对应的URL
    ];
}
//去除重复的值
function array_unique_list($arr) {
 
  $ar = array_map('serialize',$arr);
  $ar = array_unique($ar);
  return array_map('unserialize',$ar);
}
//13位时间戳
function Millisecond($time){
	list($msec,$sec) = explode ( " ",microtime ());
	$millisecond = intval($msec*1000) + $sec*1000;
	return $millisecond;
}
function generateCode()
{
	//生成只有大写字母和数字
	$chars = '0123456789';//所有字符的集合
	$len = 6;//码值的总长度
	$chars_length = strlen($chars);//字符串长度
	
	$code = '';//初始化码值
	for($i=1; $i<=$len; ++$i){//循环生成4个随机下标
		$rand_index = mt_rand(0,$chars_length-1);//得到0到长度-1之间的任意随机		$code .= $chars[$rand_index];//索引下标的字符串访问形式,拼凑码值
	}
	return $code;
}
	//日期换算
 function china_year($numYear){
		$china_era = array('0'=>'庚','1'=>'辛','2'=>'壬','3'=>'癸','4'=>'甲','5'=>'乙','6'=>'丙','7'=>'丁','8'=>'戊','9'=>'己');
		$china_branch = array('0'=>'申','1'=>'酉','2'=>'戌','3'=>'亥','4'=>'子','5'=>'丑','6'=>'寅','7'=>'卯','8'=>'辰','9'=>'巳','10'=>'午','11'=>'未');
		$lastNum = mb_strcut($numYear,-1);
		$remainder = $numYear%12;
		$chinaYear = $china_era[$lastNum].$china_branch[$remainder].'年';
		return $chinaYear;
	}
	
	//数组排序
	function list_sort_by($list,$field,$sortby='asc') {
	  if(is_array($list)){
	    $refer = $resultSet = array();
	    foreach ($list as $i => $data)
	      $refer[$i] = &$data[$field];
	    switch ($sortby) {
	      case 'asc': // 正向排序
	        asort($refer);
	        break;
	      case 'desc':// 逆向排序
	        arsort($refer);
	        break;
	      case 'nat': // 自然排序
	        natcasesort($refer);
	        break;
	    }
	    foreach ( $refer as $key=> $val)
	      $resultSet[] = &$list[$key];
	    return $resultSet;
	  }
	  return false;
 }
	//对数组$arr按照$keys进行排序,$type排序方式(asc或desc),$num返回多少项
  function arraySort($arr,$keys,$type,$num){
  	$keysvalue = $new_array = array();
  	foreach ($arr as $k=>$v){
  		$keysvalue[$k] = $v[$keys];
  	}
  	if($type == 'asc'){
  		asort($keysvalue);
  	}else{
  		arsort($keysvalue);
  	}
  	reset($keysvalue);
  	$i =0;
  	foreach ($keysvalue as $k=>$v){
  		$new_array[$i] = $arr[$k];
  		if ($i<$num){
  			$i++;
  		}else{
  			break;
  		}
  	}
  	return $new_array;
  }
//获取文章内的图片地址
  function getPicFromString($string)
  {
  	$pic = '';
  	$string = stripslashes($string);
  	$string = preg_replace("/\<img src=\".*?image\/face\/(.+?).gif\".*?\>\s*/is",'',$string);	//移除表情符
  	preg_match_all("/<[img|IMG].*?src\=[\"\'](.*?)[\"\']/i",$string,$mathes);
  	if(!empty($mathes[1]) || !empty($mathes[2])){
  		$pic_arrays = array();
  		$pic = $mathes[0];
  		foreach ($pic as $key){
  			preg_match_all("/src\=[\"\'](.*?)[\"\']/i",substr($key,5),$picture);
  			$pic_arrays[] = $picture[1][0];
  		}
  		$pic_array['exists']="y";
  		$pic_array['url']=$pic_arrays;
		}else{
  		$pic_array['exists']="n";
  	}
  	return $pic_array;
  }
 //去空格
  function delBlank($str)
  {
    $str = str_replace('&nbsp;',$str);
    $str = preg_replace("/\s(?=\s)/",$str);
    $str = preg_replace("/[\n\r\t]/",$str);
    $str = preg_replace("/ /","",$str); //匹配html中的空格
    return trim($str); //返回字符串
  
  }
 
  //去HTML标签
  function delLabel($string)
  {
  $body = preg_replace("/<(.*?)>/",$string);
  return $body;
  }
原文链接:https://www.f2er.com/regex/358027.html

猜你在找的正则表达式相关文章