用python语言实现根据pid杀死相应进程
kill_process.py代码如下
代码如下 | 复制代码 |
#! /usr/bin/python # -*- coding: utf-8 -*- importos importsys importsignal defkill(pid): try: a=os.kill(pid, signal.SIGKILL) # a = os.kill(pid, signal.9) # 与上等效 print'已杀死pid为%s的进程, 返回值是:%s'%(pid, a) exceptOSError, e: print'没有如此进程!!!' if__name__=='__main__': kill(8132) |
ok, Enjoy it !!!
这篇文章给大家介绍的是php中常用的函数,超级好用!感兴趣的小伙伴赶快来看看吧。临近下班了,大约还有20分钟左右,手头没事,给大家分享几个函数。超级好用哟!
截取字符串函数
代码如下 | 复制代码 |
/** * @param string $begin 开始字符串 * @param string $end 结束字符串 * @param string $str 需要截取的字符串 * @return string */ functionget_str($begin,$end,$str){ $b= mb_strpos($str,$begin) + mb_strlen($begin); $e= mb_strpos($str,$end) -$b; returnmb_substr($str,$b,$e); } |
这是一个非常好用的截取字符串的函数,入过是html代码,请先用strip_tags()函数将代码转为字符串!
Curl封装函数
代码如下 | 复制代码 |
functioncurlGet($url) { $ch= curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); returncurl_exec($ch); } |
写过Curl的都知道,总是要写一大堆才能使用,现在博主也给你封装好了,拿去用吧,参数应该猪也知道,所以不再标注!
分类树函数,可用于分类,和留言板等等之类的层级关系
代码如下 | 复制代码 |
/** * 定义分类树函数 * @param items 需要分类的二维数组 * @param $id 主键(唯一ID) * @param $belong_id 关联主键的PID * @son 可以自定义往里面插入就行 */ functioncatagory($items,$id='id',$belong_id='belong_id',$son='children'){ $tree=array();//格式化的树 $tmpMap=array();//临时扁平数据
foreach($itemsas$item) { $tmpMap[$item[$id]] =$item; }
foreach($itemsas$item) { if(isset($tmpMap[$item[$belong_id]])) { $tmpMap[$item[$belong_id]][$son][] = &$tmpMap[$item[$id]]; }else{ $tree[] = &$tmpMap[$item[$id]]; } } unset($tmpMap); return$tree; } |
好的~博主下班踢球去了~
bye,see you!
PHP使用strrev翻转经常出现中文乱码的问题,本文分享了解决出现中文乱码问题的方法,希望能帮助到有需要的同学本文实例讲述了PHP使用strrev翻转中文乱码问题的解决方法。分享给大家供大家参考,具体如下:
在用PHP中的strrve翻转中文时,会出现乱码情况
例如:
代码如下 | 复制代码 |
header("Content-Type: text/html; charset=utf-8"); echostrrev("处理使用strrev()函数时的乱码问题")." |
运行结果为:
代码如下 | 复制代码 |
��???䄚?氕潇�)(verrts��?䆐?r� |
解决方法就是自己重写一个cnstrrev的函数
代码如下 | 复制代码 |
header("Content-Type: text/html; charset=utf-8"); $str='处理使用strrev()函数时的乱码问题' functioncnstrrev($str) { $len=strlen($str); for($i= 0;$i<$len;$i++) { $char=$str{0}; if(ord($char) > 127)//ord()函数取得第一个字符的ASCII码,如果大于0xa0(127)的话则是中文字符 { $i+=2;//utf-8编码的情况下,一个中文字符占三个字节 if($i<$len) { $arr[] =substr($str, 0, 3);//utf-8编码的情况下,一个中文字符占三个字节 $str=substr($str, 3); } } else { $arr[] =$char; $str=substr($str, 1);//否则为非中文,占一个字符 } } returnjoin(array_reverse($arr));//以相反的元素顺序返回数组: } echo$str.'+'.cnstrrev($str); |
运行结果为:
代码如下 | 复制代码 |
处理使用strrev()函数时的乱码问题+题问码乱的时数函)(verrts用使理处 |
本文实例讲述了PHP生成图片缩略图类。分享给大家供大家参考,具体如下:
代码如下 | 复制代码 |
classApp_image_helper { protected$imgFileName; protected$imgWidth; protected$imgHeight; protected$imgMime; protected$imgResource; static $imgMineList =array( 'jpeg'=>'image/jpeg', 'gif'=>'image/gif', 'png'=>'image/png', 'wbmp'=>'image/wbmp', ); /** * 根据文件名,初始化图片, * 计算出给定图片的宽、高、图片类型,并获取图片的资源保存到内存,便于下次使用 * App_image_helper constructor. * * @param $fileName */ publicfunction__construct($fileName) { $this->imgFileName =$fileName; list($this->imgWidth,$this->imgHeight,$this->imgMime) =$this->getImageInfo($this->imgFileName); $this->imgResource =$this->getImageResource($this->imgFileName); } /** * 根据图片路径获取相关宽、高、MIME类型信息 * * @param $fileName * * @return array|null */ protectedfunctiongetImageInfo($fileName) { $result= null; if(is_file($fileName) ) { $tmpImageInfo=getimagesize($fileName); if($tmpImageInfo) { $result=array($tmpImageInfo[0],$tmpImageInfo[1],$tmpImageInfo['mime']); } } return$result; } /** * 将图片文件转为资源类类型 * * @param $fileName * * @return null|resource */ protectedfunctiongetImageResource($fileName) { $image= null; if(is_file($fileName) ) { switch($this->imgMime) { caseself::$imgMineList['jpeg']: $image= imagecreatefromjpeg($fileName); break; caseself::$imgMineList['gif']: $image= imagecreatefromgif($fileName); break; caseself::$imgMineList['png']: $image= imagecreatefrompng($fileName); break; caseself::$imgMineList['wbmp']: $image= imagecreatefromwbmp($fileName); break; default: break; } } return$image; } /** * 可根据固定宽,等比缩放图片;或根据百分比,等比缩放图片 * * @param int $width * @param int $percent * * @return array|null */ protectedfunctiongetSizeByScale($width= 360,$percent= 1) { $result= null; if($this->imgWidth &&$this->imgHeight ) { if($width) { $result=array($width,intval($width*$this->imgHeight /$this->imgWidth)); }elseif($percent) { $result=array(intval($this->imgWidth *$percent),intval($this->imgHeight *$percent)); } } return$result; } /** * 外调 * * @param int $percentOrWidth int整数表示图片缩放为固定宽度,0.0~0.99999表示缩放百分比 * @param null $fileName * @param int $quality * @param bool $reSample 重新采样图片,默认是 * * @return bool */ publicfunctioncreateImage($percentOrWidth= 1,$fileName= null,$quality= 75,$reSample= true) { $result= false; $fileName? header('Content-Type: '.$this->imgMime) : false; $size=$this->getSizeByScale(($percentOrWidth<= 1) ? null :$percentOrWidth,$percentOrWidth); if($size) { $thumb= imagecreatetruecolor($size[0],$size[1]); if($reSample) { imagecopyresampled($thumb,$this->imgResource, 0, 0, 0, 0,$size[0],$size[1],$this->imgWidth,$this->imgHeight); }else{ imagecopyresized($thumb,$this->imgResource, 0, 0, 0, 0,$size[0],$size[1],$this->imgWidth,$this->imgHeight); } $result= imagejpeg($thumb,$fileName,$quality); } return$result; } } |