当前位置: 代码迷 >> PHP >> 直接可以拿来用的PHP惯用功能代码片段(1~5)
  详细解决方案

直接可以拿来用的PHP惯用功能代码片段(1~5)

热度:254   发布时间:2016-04-28 20:21:46.0
直接可以拿来用的PHP常用功能代码片段(1~5)

文章来源:jquery教程?-?http://www.jq-school.com/Show.aspx?id=322

前面已经分享了100个常用的原生JavaScript代码片段,现在有空就开始收集PHP实用功能代码片段分享给大家用,希望可以帮到jquery学堂群里面的成员和广大对PHP开发的网友们提高开发效率,由于代码比较长,每收集5个常用的代码片段就整理成一篇文章分享出来,以下是第一篇文章。

1、PHP实现即时网站截图

转一个通过PHP实现即时网站截图功能

通过一个表单来提交请求<form action="screenshot_it.php">网站地址 (不带 http://):<input name="site" type="text" size="15" value="www."/><br/>大小: <input name="x" type="text" value="800" size="3"/>px<br /><input name="y" type="text" value="1000" size="3"/>px<br />图片格式:<select name="format"><option value="PNG">PNG</option><option value="JPEG">JPEG</option></select> <input type="submit" name="preview" value="生成截图"/></form>处理页面..<?php$x = $_REQUEST['x'];$y = $_REQUEST['y'];$format = $_REQUEST['format'];$site = $_REQUEST['site'];$surl = 'http://0907.org/screenshot/screenshot_it.php?site='.$site.'&x='.$x.'&y='.$y.'&format='.$format;if($_REQUEST['format'] == 'PNG') {	$ifm = 'png';} else {	$ifm = 'jpg';}$imt = 'image/'.$ifm;$ifn = 'screenshot.'.$ifm;if(isset($_REQUEST['preview'])) {	$iurl = 'http://域名/子目录/screenshot_it.php?site='.$site.'&x='.$x.'&y='.$y.'&format='.$format; 	$gwptitle = $_REQUEST['site'].' blog.0907.org';	include_once("../css.php");   	//这里可以删除呦!因为是CSS 	echo '<div class="status4xx">';	echo '? <b>点击图片下载截图!</div></b><br/><a href="'.$iurl.'"><img src="'.$iurl.'" width="240" height="320" /></a><br />';} else {	header("Content-type: $imt");	header("Content-Disposition: attachment; filename= $ifn");	readfile($surl);}?>


2、PHP实现汉字转整型数字,如:一百零一 转成101

test();/** * 测试 */function test() {    echo CnToInt('一'); // 1    echo CnToInt('十'); // 10    echo CnToInt('十一'); // 11    echo CnToInt('一百一十'); // 110    echo CnToInt('一千零一'); // 1001    echo CnToInt('一万零一百零一'); // 10101    echo CnToInt('一亿一千三百万零三千零一'); // 113003001    echo CnToInt('一千万亿'); // 11.0E+15}/** * 中文转数字 * @param String $var 需要解析的中文数 * @param Int $start 初始值 * @return int */function CnToInt($var, $start = 0) {    if (is_numeric($var)) {        return $var;    }    if (intval($var) === 0) {        $splits = array('亿' => 100000000, '万' => 10000);        $chars = array('万' => 10000, '千' => 1000, '百' => 100, '十' => 10, '一' => 1, '零' => 0);        $Ints = array('零' => 0, '一' => 1, '二' => 2, '三' => 3, '四' => 4, '五' => 5, '六' => 6, '七' => 7, '八' => 8, '九' => 9, '十' => 10);        $var = str_replace('零', "", $var);        foreach ($splits as $key => $step) {            if (strpos($var, $key)) {                $strs = explode($key, $var);                $start += CnToInt(array_shift($strs)) * $step;                $var = join('', $strs);            }        }        foreach ($chars as $key => $step) {            if (strpos($var, $key) !== FALSE) {                $vs = explode($key, $var);                if ($vs[0] === "") {                    $vs[0] = '一';                }                $start += $Ints[array_shift($vs)] * $step;                $var = join('', $vs);            } elseif (mb_strlen($var, 'utf-8') === 1) {                $start += $Ints[$var];                $var = '';                break;            }        }        return $start;    } else {        return intval($var);    }}


3、PHP实现简单的对称加密和解密方法

/** * 通用加密 * @param String $string 需要加密的字串 * @param String $skey 加密EKY * @return String */function enCode($string = '', $skey = 'echounion') {    $skey = array_reverse(str_split($skey));    $strArr = str_split(base64_encode($string));    $strCount = count($strArr);    foreach ($skey as $key => $value) {        $key < $strCount && $strArr[$key].=$value;    }    return str_replace('=', 'O0O0O', join('', $strArr));}/** * 通用解密 * @param String $string 需要解密的字串 * @param String $skey 解密KEY * @return String */function deCode($string = '', $skey = 'echounion') {    $skey = array_reverse(str_split($skey));    $strArr = str_split(str_replace('O0O0O', '=', $string), 2);    $strCount = count($strArr);    foreach ($skey as $key => $value) {        $key < $strCount && $strArr[$key] = rtrim($strArr[$key], $value);    }    return base64_decode(join('', $strArr));}


4、PHP实现数字转字母,生成Excel列标

/** * 数字转字母 (类似于Excel列标) * @param Int $index 索引值 * @param Int $start 字母起始值 * @return String 返回字母 */function IntToChr($index, $start = 65) {	$str = '';	if (floor($index / 26) > 0) {		$str .= IntToChr(floor($index / 26)-1);	}	return $str . chr($index % 26 + $start);}/** * 测试 */function test() {	echo IntToChr(0); //# A	echo IntToChr(1); //# B	// ...	echo IntToChr(27); //# AB}


5、PHP实现计算两个时间戳之间的时间长度

/** * 返回两个时间的相距时间,*年*月*日*时*分*秒 * @param int $one_time 时间一 * @param int $two_time 时间二 * @param int $return_type 默认值为0,0/不为0则拼接返回,1/*秒,2/*分*秒,3/*时*分*秒/,4/*日*时*分*秒,5/*月*日*时*分*秒,6/*年*月*日*时*分*秒 * @param array $format_array 格式化字符,例,array('年', '月', '日', '时', '分', '秒') * @return String or false */public function getRemainderTime($one_time, $two_time, $return_type=0, $format_array=array('年', '月', '日', '时', '分', '秒')){	if($return_type<0 || $return_type>6){		return false;	}	if(!(is_int($one_time) && is_int($two_time))){		return false;	}	$remainder_seconds = abs($one_time-$two_time);	//年	$years = 0;	if(($return_type==0 || $return_type==6) && $remainder_seconds-31536000>0){		$years = floor($remainder_seconds/(31536000));	}	//月	$monthes = 0;	if(($return_type==0 || $return_type>=5) && $remainder_seconds-$years*31536000-2592000>0){		$monthes = floor(($remainder_seconds-$years*31536000)/(2592000));	}	//日	$days = 0;	if(($return_type==0 || $return_type>=4) && $remainder_seconds-$years*31536000-$monthes*2592000-86400>0){		$days = floor(($remainder_seconds-$years*31536000-$monthes*2592000)/(86400));	}	//时	$hours = 0;	if(($return_type==0 || $return_type>=3) && $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-3600>0){		$hours = floor(($remainder_seconds-$years*31536000-$monthes*2592000-$days*86400)/3600);	}	//分	$minutes = 0;	if(($return_type==0 || $return_type>=2) && $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600-60>0){		$minutes = floor(($remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600)/60);	}	//秒	$seconds = $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600-$minutes*60;	$return = false;	switch ($return_type){		case 0:			if($years>0){				$return = $years.$format_array[0].$monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];			}else if($monthes>0){				$return = $monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];			}else if($days>0){				$return = $days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];			}else if($hours>0){				$return = $hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];			}else if($minutes>0){				$return = $minutes.$format_array[4].$seconds.$format_array[5];			}else{				$return = $seconds.$format_array[5];			}			break;            		case 1:			$return = $seconds.$format_array[5];			break;		case 2:			$return = $minutes.$format_array[4].$seconds.$format_array[5];			break;		case 3:			$return = $hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];			break;		case 4:			$return = $days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];			break;		case 5:			$return = $monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];			break;		case 6:			$return = $years.$format_array[0].$monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];			break;		default:			$return = false;                	}	return $return;}

?

  相关解决方案