这是项目中使用的PHP常用工具类大全现分享给大家/**<br>
* 助手类<br>
* @author www.shouce.ren<br>
*<br>
*/<br>
class Helper<br>
{<br>
/**<br>
* 判断当前服务器系统<br>
* @return string<br>
*/<br>
public static function getOS(){<br>
if(PATH_SEPARATOR == ':'){<br>
return 'Linux';<br>
}else{<br>
return 'Windows';<br>
}<br>
}<br>
/**<br>
* 当前微妙数<br>
* @return number<br>
*/<br>
public static function microtime_float() {<br>
list ( $usec, $sec ) = explode ( " ", microtime () );<br>
return (( float ) $usec + ( float ) $sec);<br>
}<br>
<br>
/**<br>
* 切割utf-8格式的字符串(一个汉字或者字符占一个字节)<br>
*<br>
* @author zhao jinhan<br>
* @version v1.0.0<br>
*<br>
*/<br>
public static function truncate_utf8_string($string, $length, $etc = '...') {<br>
$result = '';<br>
$string = html_entity_decode ( trim ( strip_tags ( $string ) ), ENT_QUOTES, 'UTF-8' );<br>
$strlen = strlen ( $string );<br>
for($i = 0; (($i 0)); $i ++) {<br>
if ($number = strpos ( str_pad ( decbin ( ord ( substr ( $string, $i, 1 ) ) ), 8, '0', STR_PAD_LEFT ), '0' )) {<br>
if ($length
break;<br>
}<br>
$result .= substr ( $string, $i, $number );<br>
$length -= 1.0;<br>
$i += $number - 1;<br>
} else {<br>
$result .= substr ( $string, $i, 1 );<br>
$length -= 0.5;<br>
}<br>
}<br>
$result = htmlspecialchars ( $result, ENT_QUOTES, 'UTF-8' );<br>
if ($i
$result .= $etc;<br>
}<br>
return $result;<br>
}<br>
<br>
/**<br>
* 遍历文件夹<br>
* @param string $dir<br>
* @param boolean $all true表示递归遍历<br>
* @return array<br>
*/<br>
public static function scanfDir($dir='', $all = false, &$ret = array()){<br>
if ( false !== ($handle = opendir ( $dir ))) {<br>
while ( false !== ($file = readdir ( $handle )) ) {<br>
if (!in_array($file, array('.', '..', '.git', '.gitignore', '.svn', '.htaccess', '.buildpath','.project'))) {<br>
$cur_path = $dir . '/' . $file;<br>
if (is_dir ( $cur_path )) {<br>
$ret['dirs'][] =$cur_path;<br>
$all && self::scanfDir( $cur_path, $all, $ret);<br>
} else {<br>
$ret ['files'] [] = $cur_path;<br>
}<br>
}<br>
}<br>
closedir ( $handle );<br>
}<br>
return $ret;<br>
}<br>
<br>
/**<br>
* 邮件发送<br>
* @param string $toemail<br>
* @param string $subject<br>
* @param string $message<br>
* @return boolean<br>
*/<br>
public static function sendMail($toemail = '', $subject = '', $message = '') {<br>
$mailer = Yii::createComponent ( 'application.extensions.mailer.EMailer' );<br>
<br>
//邮件配置<br>
$mailer->SetLanguage('zh_cn');<br>
$mailer->Host = Yii::app()->params['emailHost']; //发送邮件服务器<br>
$mailer->Port = Yii::app()->params['emailPort']; //邮件端口<br>
$mailer->Timeout = Yii::app()->params['emailTimeout'];//邮件发送超时时间<br>
$mailer->ContentType = 'text/html';//设置html格式<br>
$mailer->SMTPAuth = true;<br>
$mailer->Username = Yii::app()->params['emailUserName'];<br>
$mailer->Password = Yii::app()->params['emailPassword'];<br>
$mailer->IsSMTP ();<br>
$mailer->From = $mailer->Username; // 发件人邮箱<br>
$mailer->FromName = Yii::app()->params['emailFormName']; // 发件人姓名<br>
$mailer->AddReplyTo ( $mailer->Username );<br>
$mailer->CharSet = 'UTF-8';<br>
<br>
// 添加邮件日志<br>
$modelMail = new MailLog ();<br>
$modelMail->accept = $toemail;<br>
$modelMail->subject = $subject;<br>
$modelMail->message = $message;<br>
$modelMail->send_status = 'waiting';<br>
$modelMail->save ();<br>
// 发送邮件<br>
$mailer->AddAddress ( $toemail );<br>
$mailer->Subject = $subject;<br>
$mailer->Body = $message;<br>
<br>
if ($mailer->Send () === true) {<br>
$modelMail->times = $modelMail->times + 1;<br>
$modelMail->send_status = 'success';<br>
$modelMail->save ();<br>
return true;<br>
} else {<br>
$error = $mailer->ErrorInfo;<br>
$modelMail->times = $modelMail->times + 1;<br>
$modelMail->send_status = 'failed';<br>
$modelMail->error = $error;<br>
$modelMail->save ();<br>
return false;<br>
}<br>
}<br>
<br>
/**<br>
* 判断字符串是utf-8 还是gb2312<br>
* @param unknown $str<br>
* @param string $default<br>
* @return string<br>
*/<br>
public static function utf8_gb2312($str, $default = 'gb2312')<br>
{<br>
$str = preg_replace("/[\x01-\x7F]+/", "", $str);<br>
if (empty($str)) return $default;<br>
<br>
$preg = array(<br>
"gb2312" => "/^([\xA1-\xF7][\xA0-\xFE])+$/", //正则判断是否是gb2312<br>
"utf-8" => "/^[\x{4E00}-\x{9FA5}]+$/u", //正则判断是否是汉字(utf8编码的条件了),这个范围实际上已经包含了繁体中文字了<br>
);<br>
<br>
if ($default == 'gb2312') {<br>
$option = 'utf-8';<br>
} else {<br>
$option = 'gb2312';<br>
}<br>
<br>
if (!preg_match($preg[$default], $str)) {<br>
return $option;<br>
}<br>
$str = @iconv($default, $option, $str);<br>
<br>
//不能转成 $option, 说明原来的不是 $default<br>
if (empty($str)) {<br>
return $option;<br>
}<br>
return $default;<br>
}<br>
/**<br>
* utf-8和gb2312自动转化<br>
* @param unknown $string<br>
* @param string $outEncoding<br>
* @return unknown|string<br>
*/<br>
public static function safeEncoding($string,$outEncoding = 'UTF-8')<br>
{<br>
$encoding = "UTF-8";<br>
for($i = 0; $i
if (ord ( $string {$i} )
continue;<br>
<br>
if ((ord ( $string {$i} ) & 224) == 224) {<br>
// 第一个字节判断通过<br>
$char = $string {++ $i};<br>
if ((ord ( $char ) & 128) == 128) {<br>
// 第二个字节判断通过<br>
$char = $string {++ $i};<br>
if ((ord ( $char ) & 128) == 128) {<br>
$encoding = "UTF-8";<br>
break;<br>
}<br>
}<br>
}<br>
if ((ord ( $string {$i} ) & 192) == 192) {<br>
// 第一个字节判断通过<br>
$char = $string {++ $i};<br>
if ((ord ( $char ) & 128) == 128) {<br>
// 第二个字节判断通过<br>
$encoding = "GB2312";<br>
break;<br>
}<br>
}<br>
}<br>
<br>
if (strtoupper ( $encoding ) == strtoupper ( $outEncoding ))<br>
return $string;<br>
else<br>
return @iconv ( $encoding, $outEncoding, $string );<br>
}<br>
/**<br>
* 返回二维数组中某个键名的所有值<br>
* @param input $array<br>
* @param string $key<br>
* @return array<br>
*/<br>
public static function array_key_values($array =array(), $key='')<br>
{<br>
$ret = array();<br>
foreach((array)$array as $k=>$v){<br>
$ret[$k] = $v[$key];<br>
}<br>
return $ret;<br>
}<br>
<br>
<br>
/**<br>
* 判断 文件/目录 是否可写(取代系统自带的 is_writeable 函数)<br>
* @param string $file 文件/目录<br>
* @return boolean<br>
*/<br>
public static function is_writeable($file) {<br>
if (is_dir($file)){<br>
$dir = $file;<br>
if ($fp = @fopen("$dir/test.txt", 'w')) {<br>
@fclose($fp);<br>
@unlink("$dir/test.txt");<br>
$writeable = 1;<br>
} else {<br>
$writeable = 0;<br>
}<br>
} else {<br>
if ($fp = @fopen($file, 'a+')) {<br>
@fclose($fp);<br>
$writeable = 1;<br>
} else {<br>
$writeable = 0;<br>
}<br>
}<br>
<br>
return $writeable;<br>
}<br>
/**<br>
* 格式化单位<br>
*/<br>
static public function byteFormat( $size, $dec = 2 ) {<br>
$a = array ( "B" , "KB" , "MB" , "GB" , "TB" , "PB" );<br>
$pos = 0;<br>
while ( $size >= 1024 ) {<br>
$size /= 1024;<br>
$pos ++;<br>
}<br>
return round( $size, $dec ) . " " . $a[$pos];<br>
}<br>
<br>
/**<br>
* 下拉框,单选按钮 自动选择<br>
*<br>
* @param $string 输入字符<br>
* @param $param 条件<br>
* @param $type 类型<br>
* selected checked<br>
* @return string<br>
*/<br>
static public function selected( $string, $param = 1, $type = 'select' ) {<br>
<br>
$true = false;<br>
if ( is_array( $param ) ) {<br>
$true = in_array( $string, $param );<br>
}elseif ( $string == $param ) {<br>
$true = true;<br>
}<br>
$return='';<br>
if ( $true )<br>
$return = $type == 'select' ? 'selected="selected"' : 'checked="checked"';<br>
<br>
echo $return;<br>
}<br>
<br>
/**<br>
* 下载远程图片<br>
* @param string $url 图片的绝对url<br>
* @param string $filepath 文件的完整路径(例如/www/images/test) ,此函数会自动根据图片url和http头信息确定图片的后缀名<br>
* @param string $filename 要保存的文件名(不含扩展名)<br>
* @return mixed 下载成功返回一个描述图片信息的数组,下载失败则返回false<br>
*/<br>
static public function downloadImage($url, $filepath, $filename) {<br>
//服务器返回的头信息<br>
$responseHeaders = array();<br>
//原始图片名<br>
$originalfilename = '';<br>
//图片的后缀名<br>
$ext = '';<br>
$ch = curl_init($url);<br>
//设置curl_exec返回的值包含Http头<br>
curl_setopt($ch, CURLOPT_HEADER, 1);<br>
//设置curl_exec返回的值包含Http内容<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>
//设置抓取跳转(http 301,302)后的页面<br>
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);<br>
//设置最多的HTTP重定向的数量<br>
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);<br>
<br>
//服务器返回的数据(包括http头信息和内容)<br>
$html = curl_exec($ch);<br>
//获取此次抓取的相关信息<br>
$httpinfo = curl_getinfo($ch);<br>
curl_close($ch);<br>
if ($html !== false) {<br>
//分离response的header和body,由于服务器可能使用了302跳转,所以此处需要将字符串分离为 2+跳转次数 个子串<br>
$httpArr = explode("\r\n\r\n", $html, 2 + $httpinfo['redirect_count']);<br>
//倒数第二段是服务器最后一次response的http头<br>
$header = $httpArr[count($httpArr) - 2];<br>
//倒数第一段是服务器最后一次response的内容<br>
$body = $httpArr[count($httpArr) - 1];<br>
$header.="\r\n";<br>
<br>
//获取最后一次response的header信息<br>
preg_match_all('/([a-z0-9-_]+):\s*([^\r\n]+)\r\n/i', $header, $matches);<br>
if (!empty($matches) && count($matches) == 3 && !empty($matches[1]) && !empty($matches[1])) {<br>
for ($i = 0; $i
if (array_key_exists($i, $matches[2])) {<br>
$responseHeaders[$matches[1][$i]] = $matches[2][$i];<br>
}<br>
}<br>
}<br>
//获取图片后缀名<br>
if (0
$originalfilename = $matches[0];<br>
$ext = $matches[1];<br>
} else {<br>
if (array_key_exists('Content-Type', $responseHeaders)) {<br>
if (0
$ext = $extmatches[1];<br>
}<br>
}<br>
}<br>
//保存文件<br>
if (!empty($ext)) {<br>
//如果目录不存在,则先要创建目录<br>
if(!is_dir($filepath)){<br>
mkdir($filepath, 0777, true);<br>
}<br>
<br>
$filepath .= '/'.$filename.".$ext";<br>
$local_file = fopen($filepath, 'w');<br>
if (false !== $local_file) {<br>
if (false !== fwrite($local_file, $body)) {<br>
fclose($local_file);<br>
$sizeinfo = getimagesize($filepath);<br>
return array('filepath' => realpath($filepath), 'width' => $sizeinfo[0], 'height' => $sizeinfo[1], 'orginalfilename' => $originalfilename, 'filename' => pathinfo($filepath, PATHINFO_BASENAME));<br>
}<br>
}<br>
}<br>
}<br>
return false;<br>
}
1000多行代码呢太多了保存发不了,查看并下载完整工具类请到http://www.shouce.ren/post/view/id/1700
AD:真正免费,域名+虚机+企业邮箱=0元