PHP分页实例代码

 更新时间:2016年11月25日 16:25  点击:2250

PHP分页实例代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>php教程 make page list</title>
<style type="text/CSS">
<!--
.page a:link {
 color: #0000FF;
 text-decoration: none;
}
.page a:visited {
 text-decoration: none;
 color: #0000FF;
}
.page a:hover {
 text-decoration: none;
 color: #0000FF;
}
.page a:active {
 text-decoration: none;
 color: #0000FF;
}
.page{color:#0000FF;}
-->
</style>
</head>
<body>
<table width="530" height="103" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  <tr>
    <th width="30" height="38" bgcolor="#E3E3E3" scope="col">ID</th>
    <th width="500" bgcolor="#E3E3E3" scope="col">文章标题</th>
  </tr>
<?php
/*
 * Created on 2010-4-17
 *
 * Order by Kove Wong
 */
 $link=MySQL_connect(’localhost’,’root’,’haoxiazai1987’);
 mysql教程_select_db(’pagelist’);
 mysql_query(’set names gbk’);

 $Page_size=10;

 $result=mysql_query(’select * from v_char’);
 $count = mysql_num_rows($result);
 $page_count  = ceil($count/$Page_size);

 $init=1;
 $page_len=7;
 $max_p=$page_count;
 $pages=$page_count;

 //判断当前页码
 if(empty($_GET[’page’])||$_GET[’page’]<0){
  $page=1;
 }else {
 $page=$_GET[’page’];
}

 $offset=$Page_size*($page-1);
 $sql="select * from v_char limit $offset,$Page_size";
 $result=mysql_query($sql,$link);
 while ($row=mysql_fetch_array($result)) {
?>
  <tr>
    <td bgcolor="#E0EEE0" height="25px"><div align="center">
      <?php echo $row[’id’]?>
    </div></td>
    <td bgcolor="#E0EEE"><div align="center">
      <?php echo $row[’name’]?>
    </div></td>
  </tr>
<?php
}
 $page_len = ($page_len%2)?$page_len:$pagelen+1;//页码个数
 $pageoffset = ($page_len-1)/2;//页码个数左右偏移量

 $key=’<div class="page">’;
 $key.="<span>$page/$pages</span>&nbsp;";   //第几页,共几页
 if($page!=1){
 $key.="<a href="".$_SERVER[’PHP_SELF’]."?page=1">第一页</a> ";    //第一页
 $key.="<a href="".$_SERVER[’PHP_SELF’]."?page=".($page-1)."">上一页</a>"; //上一页
}else {
 $key.="第一页 ";//第一页
 $key.="上一页"; //上一页
}
 if($pages>$page_len){
 //如果当前页小于等于左偏移
 if($page<=$pageoffset){
 $init=1;
 $max_p = $page_len;
 }else{//如果当前页大于左偏移
 //如果当前页码右偏移超出最大分页数
 if($page+$pageoffset>=$pages+1){
 $init = $pages-$page_len+1;
 }else{
 //左右偏移都存在时的计算
 $init = $page-$pageoffset;
 $max_p = $page+$pageoffset;
 }
 }
  }
  for($i=$init;$i<=$max_p;$i++){
 if($i==$page){
 $key.=’ <span>’.$i.’</span>’;
 } else {
 $key.=" <a href="".$_SERVER[’PHP_SELF’]."?page=".$i."">".$i."</a>";
 }
  }
  if($page!=$pages){
 $key.=" <a href="".$_SERVER[’PHP_SELF’]."?page=".($page+1)."">下一页</a> ";//下一页
 $key.="<a href="".$_SERVER[’PHP_SELF’]."?page={$pages}">最后一页</a>"; //最后一页
 }else {
 $key.="下一页 ";//下一页
 $key.="最后一页"; //最后一页
 }
 $key.=’</div>’;
?>
 <tr>
    <td colspan="2" bgcolor="#E0EEE0"><div align="center"><?php echo $key?></div></td>
  </tr>
</table>
</body>
</html>

这里是来自网络朋友的一个实现的文件上传类代码,我们详细的介绍了每个变量的用处,下面看看吧,有需要可以参考一下。

这里是来自网络朋友的一个实现的文件上传类代码,我们详细的介绍了每个变量的用处,下面看看吧,有需要可以参考一下。

<?php教程
 /**
  * 文件上传类
  */
 class uploadFile {

  public $max_size = '1000000';//设置上传文件大小
  public $file_name = 'date';//重命名方式代表以时间命名,其他则使用给予的名称
  public $allow_types;//允许上传的文件扩展名,不同文件类型用“|”隔开
  public $errmsg = '';//错误信息
  public $uploaded = '';//上传后的文件名(包括文件路径)
  public $save_path;//上传文件保存路径
  private $files;//提交的等待上传文件
  private $file_type = array();//文件类型
  private $ext = '';//上传文件扩展名

  /**
   * 构造函数,初始化类
   * @access public
   * @param string $file_name 上传后的文件名
   * @param string $save_path 上传的目标文件夹
   */
  public function __construct($save_path = './upload/',$file_name = 'date',$allow_types = '') {
  $this->file_name   = $file_name;//重命名方式代表以时间命名,其他则使用给予的名称
  $this->save_path   = (preg_match('//$/',$save_path)) ? $save_path : $save_path . '/';
  $this->allow_types = $allow_types == '' ? 'jpg|gif|png|zip|rar' : $allow_types;
  }

  /**
   * 上传文件
   * @access public
   * @param $files 等待上传的文件(表单传来的$_FILES[])
   * @return boolean 返回布尔值
   */
 public function upload_file($files) {
  $name = $files['name'];
  $type = $files['type'];
  $size = $files['size'];
  $tmp_name = $files['tmp_name'];
  $error = $files['error'];

  switch ($error) {
   case 0 : $this->errmsg = '';
    break;
   case 1 : $this->errmsg = '超过了php.ini中文件大小';
    break;
   case 2 : $this->errmsg = '超过了MAX_FILE_SIZE 选项指定的文件大小';
    break;
       case 3 : $this->errmsg = '文件只有部分被上传';
    break;
   case 4 : $this->errmsg = '没有文件被上传';
    break;
   case 5 : $this->errmsg = '上传文件大小为0';
    break;
      default : $this->errmsg = '上传文件失败!';
    break;
   }
  if($error == 0 && is_uploaded_file($tmp_name)) {
   //检测文件类型
   if($this->check_file_type($name) == FALSE){
    return FALSE;
   }
   //检测文件大小
   if($size > $this->max_size){
    $this->errmsg = '上传文件<font color=red>'.$name.'</font>太大,最大支持<font color=red>'.ceil($this->max_size/1024).'</font>kb的文件';
    return FALSE;
   }
   $this->set_save_path();//设置文件存放路径
   $new_name = $this->file_name != 'date' ? $this->file_name.'.'.$this->ext : date('YmdHis').'.'.$this->ext;//设置新文件名
   $this->uploaded = $this->save_path.$new_name;//上传后的文件名
   //移动文件
   if(move_uploaded_file($tmp_name,$this->uploaded)){
    $this->errmsg = '文件<font color=red>'.$this->uploaded.'</font>上传成功!';
    return TRUE;
   }else{
    $this->errmsg = '文件<font color=red>'.$this->uploaded.'</font>上传失败!';
    return FALSE;
   }

  }
 }

  /**
   * 检查上传文件类型
   * @access public
   * @param string $filename 等待检查的文件名
   * @return 如果检查通过返回TRUE 未通过则返回FALSE和错误消息
   */
    public function check_file_type($filename){
  $ext = $this->get_file_type($filename);
  $this->ext = $ext;
    $allow_types = explode('|',$this->allow_types);//分割允许上传的文件扩展名为数组
    //echo $ext;
    //检查上传文件扩展名是否在请允许上传的文件扩展名中
    if(in_array($ext,$allow_types)){
     return TRUE;
    }else{
     $this->errmsg = '上传文件<font color=red>'.$filename.'</font>类型错误,只支持上传<font color=red>'.str_replace('|',',',$this->allow_types).'</font>等文件类型!';
     return FALSE;
    }
    }

    /**
     * 取得文件类型
     * @access public
     * @param string $filename 要取得文件类型的目标文件名
     * @return string 文件类型
     */
    public function get_file_type($filename){
     $info = pathinfo($filename);
     $ext = $info['extension'];
     return $ext;
    }

 /**
  * 设置文件上传后的保存路径
  */
 public function set_save_path(){
  $this->save_path = (preg_match('//$/',$this->save_path)) ? $this->save_path : $this->save_path . '/';
  if(!is_dir($this->save_path)){
   //如果目录不存在,创建目录
   $this->set_dir();
  }
 }


 /**
  * 创建目录
  * @access public
  * @param string $dir 要创建目录的路径
  * @return boolean 失败时返回错误消息和FALSE
  */
 public function set_dir($dir = null){
  //检查路径是否存在
  if(!$dir){
   $dir = $this->save_path;
  }
  if(is_dir($dir)){
   $this->errmsg = '需要创建的文件夹已经存在!';
  }
  $dir = explode('/', $dir);
  foreach($dir as $v){
   if($v){
    $d .= $v . '/';
    if(!is_dir($d)){
     $state = mkdir($d, 0777);
     if(!$state)
      $this->errmsg = '在创建目录<font color=red>' . $d . '时出错!';
    }
   }
  }
  return true;
 }
 }

/*************************************************
 * 图片处理类
 *
 * 可以对图片进行生成缩略图,打水印等操作
 * 本类默认编码为UTF8 如果要在GBK下使用请将img_mark方法中打中文字符串水印iconv注释去掉
 *
 * 由于UTF8汉字和英文字母大小(像素)不好确定,在中英文混合出现太多时可能会出现字符串偏左
 * 或偏右,请根据项目环境对get_mark_xy方法中的$strc_w = strlen($this->mark_str)*7+5进
 * 行调整
 * 需要GD库支持,为更好使用本类推荐使用GD库2.0+
 *
 * @author kickflip@php100 QQ263340607
 *************************************************/

 class uploadImg extends uploadFile {

 public $mark_str = 'kickflip@php100';  //水印字符串
 public $str_r = 0; //字符串颜色R
 public $str_g = 0; //字符串颜色G
 public $str_b = 0; //字符串颜色B
 public $mark_ttf = './upload/SIMSUN.TTC'; //水印文字字体文件(包含路径)
 public $mark_logo = './upload/logo.png';    //水印图片
 public $resize_h;//生成缩略图高
 public $resize_w;//生成缩略图宽
 public $source_img;//源图片文件
 public $dst_path = './upload/';//缩略图文件存放目录,不填则为源图片存放目录

 /**
  * 生成缩略图 生成后的图
  * @access public
  * @param integer $w 缩小后图片的宽(px)
  * @param integer $h 缩小后图片的高(px)
  * @param string $source_img 源图片(路径+文件名)
  */
 public function img_resized($w,$h,$source_img = NULL){
  $source_img = $source_img == NULL ? $this->uploaded : $source_img;//取得源文件的地址,如果为空则默认为上次上传的图片
  if(!is_file($source_img)) { //检查源图片是否存在
   $this->errmsg = '文件'.$source_img.'不存在';
   return FALSE;
  }
  $this->source_img = $source_img;
  $img_info = getimagesize($source_img);
  $source = $this->img_create($source_img); //创建源图片
  $this->resize_w = $w;
  $this->resize_h = $h;
  $thumb = imagecreatetruecolor($w,$h);
  imagecopyresized($thumb,$source,0,0,0,0,$w,$h,$img_info[0],$img_info[1]);//生成缩略图片
  $dst_path = $this->dst_path == '' ? $this->save_path : $this->dst_path; //取得目标文件夹路径
  $dst_path = (preg_match('//$/',$dst_path)) ? $dst_path : $dst_path . '/';//将目标文件夹后加上/
  if(!is_dir($dst_path)) $this->set_dir($dst_path); //如果不存在目标文件夹则创建
  $dst_name = $this->set_newname($source_img);
  $this->img_output($thumb,$dst_name);//输出图片
  imagedestroy($source);
  imagedestroy($thumb);
 }

 /**
  *打水印
  *@access public
  *@param string $source_img 源图片路径+文件名
  *@param integer $mark_type 水印类型(1为英文字符串,2为中文字符串,3为图片logo,默认为英文字符串)
  *@param integer $mark_postion 水印位置(1为左下角,2为右下角,3为左上角,4为右上角,默认为右下角);
  *@return 打上水印的图片
  */
 public function img_mark($source_img = NULL,$mark_type = 1,$mark_postion = 2) {
  $source_img = $source_img == NULL ? $this->uploaded : $source_img;//取得源文件的地址,如果为空则默认为上次上传的图片
  if(!is_file($source_img)) { //检查源图片是否存在
   $this->errmsg = '文件'.$source_img.'不存在';
   return FALSE;
  }
  $this->source_img = $source_img;
  $img_info = getimagesize($source_img);
  $source = $this->img_create($source_img); //创建源图片
  $mark_xy = $this->get_mark_xy($mark_postion);//取得水印位置
  $mark_color = imagecolorallocate($source,$this->str_r,$this->str_g,$this->str_b);

  switch($mark_type) {

   case 1 : //加英文字符串水印
   $str = $this->mark_str;
   imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);
   $this->img_output($source,$source_img);
   break;

            case 2 : //加中文字符串水印
            if(!is_file($this->mark_ttf)) { //检查字体文件是否存在
    $this->errmsg = '打水印失败:字体文件'.$this->mark_ttf.'不存在!';
   return FALSE;
   }
   $str = $this->mark_str;
   //$str = iconv('gbk','utf-8',$str);//转换字符编码 如果使用GBK编码请去掉此行注释
   imagettftext($source,12,0,$mark_xy[2],$mark_xy[3],$mark_color,$this->mark_ttf,$str);
   $this->img_output($source,$source_img);
   break;

   case 3 : //加图片水印
   if(is_file($this->mark_logo)){  //如果存在水印logo的图片则取得logo图片的基本信息,不存在则退出
    $logo_info = getimagesize($this->mark_logo);
   }else{
    $this->errmsg = '打水印失败:logo文件'.$this->mark_logo.'不存在!';
    return FALSE;
   }

   $logo_info = getimagesize($this->mark_logo);
   if($logo_info[0]>$img_info[0] || $logo_info[1]>$img_info[1]) { //如果源图片小于logo大小则退出
    $this->errmsg = '打水印失败:源图片'.$this->source_img.'比'.$this->mark_logo.'小!';
    return FALSE;
   }

   $logo = $this->img_create($this->mark_logo);
   imagecopy ( $source, $logo, $mark_xy[4], $mark_xy[5], 0, 0, $logo_info[0], $logo_info[1]);
   $this->img_output($source,$source_img);
   break;

   default: //其它则为文字图片
   $str = $this->mark_str;
   imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);
   $this->img_output($source,$source_img);
   break;
  }
  imagedestroy($source);
 }

 /**
  * 取得水印位置
  * @access private
  * @param integer $mark_postion 水印的位置(1为左下角,2为右下角,3为左上角,4为右上角,其它为右下角)
  * @return array $mark_xy 水印位置的坐标(索引0为英文字符串水印坐标X,索引1为英文字符串水印坐标Y,
  * 索引2为中文字符串水印坐标X,索引3为中文字符串水印坐标Y,索引4为水印图片坐标X,索引5为水印图片坐标Y)
  */
 private function get_mark_xy($mark_postion){
  $img_info = getimagesize($this->source_img);

  $stre_w = strlen($this->mark_str)*9+5 ; //水印英文字符串的长度(px)(5号字的英文字符大小约为9px 为了美观再加5px)
  //(12号字的中文字符大小为12px,在utf8里一个汉字长度为3个字节一个字节4px 而一个英文字符长度一个字节大小大约为9px
  // 为了在中英文混合的情况下显示完全 设它的长度为字节数*7px)
  $strc_w = strlen($this->mark_str)*7+5 ; //水印中文字符串的长度(px)

  if(is_file($this->mark_logo)){ //如果存在水印logo的图片则取得logo图片的基本信息
   $logo_info = getimagesize($this->mark_logo);
  }

  //由于imagestring函数和imagettftext函数中对于字符串开始位置不同所以英文和中文字符串的Y位置也有所不同
  //imagestring函数是从文字的左上角为参照 imagettftext函数是从文字左下角为参照
  switch($mark_postion){

   case 1: //位置左下角
   $mark_xy[0] = 5; //水印英文字符串坐标X
   $mark_xy[1] = $img_info[1]-20;//水印英文字符串坐标Y
   $mark_xy[2] = 5; //水印中文字符串坐标X
   $mark_xy[3] = $img_info[1]-5;//水印中文字符串坐标Y
   $mark_xy[4] = 5;//水印图片坐标X
   $mark_xy[5] = $img_info[1]-$logo_info[1]-5;//水印图片坐标Y
   break;

   case 2: //位置右下角
   $mark_xy[0] = $img_info[0]-$stre_w; //水印英文字符串坐标X
   $mark_xy[1] = $img_info[1]-20;//水印英文字符串坐标Y
   $mark_xy[2] = $img_info[0]-$strc_w; //水印中文字符串坐标X
   $mark_xy[3] = $img_info[1]-5;//水印中文字符串坐标Y
   $mark_xy[4] = $img_info[0]-$logo_info[0]-5;//水印图片坐标X
   $mark_xy[5] = $img_info[1]-$logo_info[1]-5;//水印图片坐标Y
   break;

   case 3: //位置左上角
   $mark_xy[0] = 5; //水印英文字符串坐标X
   $mark_xy[1] = 5;//水印英文字符串坐标Y
   $mark_xy[2] = 5; //水印中文字符串坐标X
   $mark_xy[3] = 15;//水印中文字符串坐标Y
   $mark_xy[4] = 5;//水印图片坐标X
   $mark_xy[5] = 5;//水印图片坐标Y
   break;

   case 4: //位置右上角
   $mark_xy[0] = $img_info[0]-$stre_w; //水印英文字符串坐标X
   $mark_xy[1] = 5;//水印英文字符串坐标Y
   $mark_xy[2] = $img_info[0]-$strc_w; //水印中文字符串坐标X
   $mark_xy[3] = 15;//水印中文字符串坐标Y
   $mark_xy[4] = $img_info[0]-$logo_info[0]-5;//水印图片坐标X
   $mark_xy[5] = 5;//水印图片坐标Y
   break;

   default : //其它默认为右下角
   $mark_xy[0] = $img_info[0]-$stre_w; //水印英文字符串坐标X
   $mark_xy[1] = $img_info[1]-5;//水印英文字符串坐标Y
   $mark_xy[2] = $img_info[0]-$strc_w; //水印中文字符串坐标X
   $mark_xy[3] = $img_info[1]-15;//水印中文字符串坐标Y
   $mark_xy[4] = $img_info[0]-$logo_info[0]-5;//水印图片坐标X
   $mark_xy[5] = $img_info[1]-$logo_info[1]-5;//水印图片坐标Y
   break;
  }
  return $mark_xy;
 }

 /**
  * 创建源图片
  * @access private
  * @param string $source_img 源图片(路径+文件名)
  * @return img 从目标文件新建的图像
  */
 private function img_create($source_img) {
  $info = getimagesize($source_img);
  switch ($info[2]){
            case 1:
            if(!function_exists('imagecreatefromgif')){
             $source = @imagecreatefromjpeg($source_img);
            }else{
             $source = @imagecreatefromgif($source_img);
            }
            break;
            case 2:
            $source = @imagecreatefromjpeg($source_img);
            break;
            case 3:
            $source = @imagecreatefrompng($source_img);
            break;
            case 6:
            $source = @imagecreatefromwbmp($source_img);
            break;
            default:
   $source = FALSE;
   break;
        }
  return $source;
 }

 /**
  * 重命名图片
  * @access private
  * @param string $source_img 源图片路径+文件名
  * @return string $dst_name 重命名后的图片名(路径+文件名)
  */
 private function set_newname($sourse_img) {
  $info = pathinfo($sourse_img);
  $new_name = $this->resize_w.'_'.$this->resize_h.'_'.$info['basename'];//将文件名修改为:宽_高_文件名
  if($this->dst_path == ''){ //如果存放缩略图路径为空则默认为源文件同文件夹
   $dst_name = str_replace($info['basename'],$new_name,$sourse_img);
  }else{
   $dst_name = $this->dst_path.$new_name;
  }
  return $dst_name;
 }

 /**
  * 输出图片
  * @access private
  * @param $im 处理后的图片
  * @param $dst_name 输出后的的图片名(路径+文件名)
  * @return 输出图片
  */
 public function img_output($im,$dst_name) {
  $info = getimagesize($this->source_img);
  switch ($info[2]){
            case 1:
            if(!function_exists('imagegif')){
             imagejpeg($im,$dst_name);
            }else{
             imagegif($im, $dst_name);
            }
            break;
            case 2:
            imagejpeg($im,$dst_name);
            break;
            case 3:
            imagepng($im,$dst_name);
            break;
            case 6:
            imagewbmp($im,$dst_name);
            break;
        }
 }

 }
?>


这个写成了上传文件类就方便多了,把上面代码保存一个文件它就可以公共调用与修改删除了。

下面我们利用windown com组件来实现doc文件一些简单的操作,希望对你有所帮助。

<?php教程
// 建立一个指向新COM组件的索引
$word = new COM("word.application") or die("Can't start Word!");
// 显示目前正在使用的Word的版本号
//echo “Loading Word, v. {$word->Version}<br>”;
// 把它的可见性设置为0(假),如果要使它在最前端打开,使用1(真)
// to open the application in the forefront, use 1 (true)
//$word->Visible = 0;
echo $word->Version;
//打?一个文档
$word->Documents->OPen("C:wampwwwtest1.doc");
//读取文档内容

$test= $word->ActiveDocument->content->Text;

echo $test;
echo '<br>';
//将文档中需要换的变量更换一下
$test=str_replace('<{变量}>','这是变量',$test);
echo $test;
$word->Documents->Add();
// 在新文档中添加文字
$word->Selection->TypeText('$test');
//把文档保存在目录中
$word->Documents[1]->SaveAs("C:wampwwwtest2.doc");
// 关闭与COM组件之间的连接
$word->Quit();
?>

表四:Socket函数
函数名      描述
socket_accept()    接受一个Socket连接
socket_bind()     把socket绑定在一个IP地址和端口上
socket_clear_error()   清除socket的错误或者最后的错误代码
socket_close()     关闭一个socket资源
socket_connect()    开始一个socket连接
socket_create_listen()   在指定端口打开一个socket监听
socket_create_pair()   产生一对没有区别的socket到一个数组里
socket_create()    产生一个socket,相当于产生一个socket的数据结构
socket_get_option()    获取socket选项
socket_getpeername()   获取远程类似主机的ip地址
socket_getsockname()   获取本地socket的ip地址
socket_iovec_add()    添加一个新的向量到一个分散/聚合的数组
socket_iovec_alloc()   这个函数创建一个能够发送接收读写的iovec数据结构
socket_iovec_delete()   删除一个已经分配的iovec
socket_iovec_fetch()   返回指定的iovec资源的数据
socket_iovec_free()    释放一个iovec资源
socket_iovec_set()    设置iovec的数据新值
socket_last_error()    获取当前socket的最后错误代码
socket_listen()     监听由指定socket的所有连接
socket_read()     读取指定长度的数据
socket_readv()     读取从分散/聚合数组过来的数据
socket_recv()     从socket里结束数据到缓存
socket_recvfrom()    接受数据从指定的socket,如果没有指定则默认当前socket
socket_recvmsg()    从iovec里接受消息
socket_select()     多路选择
socket_send()     这个函数发送数据到已连接的socket
socket_sendmsg()    发送消息到socket
socket_sendto()    发送消息到指定地址的socket
socket_set_block()    在socket里设置为块模式
socket_set_nonblock()   socket里设置为非块模式
socket_set_option()    设置socket选项
socket_shutdown()    这个函数允许你关闭读、写、或者指定的socket
socket_strerror()    返回指定错误号的详细错误
socket_write()     写数据到socket缓存
socket_writev()    写数据到分散/聚合数组

更多详细内容请查看:php教程er/30/7cadb3c9195ac7d8ac9104da61a25c6e.htm">http://www.111cn.net/phper/30/7cadb3c9195ac7d8ac9104da61a25c6e.htm
<?php 
//post.php
function Post($host,$port)
{
//$host="127.0.0.1";
//建立连接
$conn = fsockopen($host,$port);
if (!$conn)
{
die("Con error");
}
//循环发送5次数据
//
for($i = 0;$i<5;$i++)
{
$data="user_name=admin".$i;
WriteData($conn,$host,$data);
echo $i."<br />";
}

fclose($conn);
}

function WriteData($conn,$host,$data)
{
$header = "POST /test.php HTTP/1.1rn";
$header.= "Host : {$host}rn";
$header.= "Content-type: application/x-www-form-urlencodedrn";
$header.= "Content-Length:".strlen($data)."rn";
//Keep-Alive是关键
$header.= "Connection: Keep-Alivernrn";
$header.= "{$data}rnrn";

fwrite($conn,$header);

//取结果
//$result = '';
//while(!feof($conn))
//{
// $result .= fgets($conn,128);
//}
//return $result;
}

Post('127.0.0.1',80);

?>
<?php 
//test.php
$fp = fopen('result.txt','a');
$data = $_POST['user_name']." -- ". date('Y-m-d H:i:s')."rn";
fwrite($fp,$data);
fclose($fp);
?>
再模仿post实现用户登录
socket.php
<?php
/**
* @author macopad@qq.com
* 模拟socket发送post方式发送数据
* 发送文件为socket.php
* 接收数据为get_socket.php
* @var unknown_type
*/
$flag = 0;
//要post的数据
$argv = array(
‘username’=>’macopad@qq.com’,
‘password’=>’macopad’
);
//构造要 post的字符串
$params = ”;
foreach ($argv as $key=>$value)
{
if ($flag!=0)
{
$params .= ”&”;
$flag = 1;
}
$params.= $key.”=”;
$params.= urlencode($value);
$flag = 1;
}
$length = strlen($params);//post的长度
//创建socket连接
$post = fsockopen($HTTP_SERVER_VARS["SERVER_ADDR"],80,$errno,$errstr,10) or exit($errstr.”—>”.$errno);
//构造post请求的头
$header = ”POST /guojinyong/test/get_socket.php HTTP/1.1rn”; //制定为 POST的方法提交数据 及要提交到的页面和协议类型
$header .= ”Host:”.$HTTP_SERVER_VARS["SERVER_ADDR"].”rn”;   //定义主机
$header .= ”Referer:http://”.$HTTP_SERVER_VARS["SERVER_ADDR"].”/guojinyong/test/socket.phprn”; //Referer信息,
$header .= ”Content-Type: application/x-www-form-urlencodedrn”; //说明这个请求为POST
$header .= ”Content-Length: ”.$length.”rn”; //提交的数据长度
$header .= ”Connection: Closernrn”;//关闭连接
$header .= $params.”rn”;//添加post的字符串
//发送post的数据
fputs($post,$header);
//接收get_socket.php返回的数据并打印出来
while(!feof($post))
{
echo fgets($post,1024);//从1024个字节之后开始获取
}
fclose($post); //关闭socket连接
?>

get_socket.php
<?php
echo ”Set-Cookie:name=Macopad; expires=Fri 12-Nov-99 3:59:59 GMT”;
$userName = ”";
$password = ”";
$userName = $_POST['username'];
$password = $_POST['password'];
echo ”<br>通过socket模拟程序发送数据!<br>”;
echo ”当前服务器是:”.$HTTP_SERVER_VARS["SERVER_ADDR"].”<br>”;
echo ”接受到的用户名是:” .$userName.”<br>接收到的密码是:”.$password;
显示结果
HTTP/1.1 200 OK Date: Wed, 14 Apr 2010 06:49:07 GMT Server: Apache X-Powered-By: PHP/5.2.5 Cache-Control: max-age=0 Expires: Wed, 14 Apr 2010 06:49:07 GMT Vary: Accept-Encoding Content-Length: 189 Connection: close Content-Type: text/html Set-Cookie:name=Macopad; expires=Fri 12-Nov-99 3:59:59 GMT
通过socket模拟程序发送数据!
当前服务器是:http://www.111cn.net
接受到的用户名是:macopad@qq.com
接收到的密码是:macopad

以前有用过dedecms分词功能,经过测试还是不理想,后来经过一些处理得到的结果还是可以接受的,今天我再看到这款分词法,拿出来给大家看看。

<?php教程
class NLP{
private static $cmd_path;
// 不以'/'结尾
static function set_cmd_path($path){
self::$cmd_path = $path;
}
private function cmd($str){
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
);
$cmd = self::$cmd_path . "/ictclas";
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
$str = iconv('utf-8', 'gbk', $str);
fwrite($pipes[0], $str);
$output = stream_get_contents($pipes[1]);
fclose($pipes[0]);
fclose($pipes[1]);
$return_value = proc_close($process);
}
/*
$cmd = "printf '$input' | " . self::$cmd_path . "/ictclas";
exec($cmd, $output, $ret);
$output = join("n", $output);
*/
$output = trim($output);
$output = iconv('gbk', 'utf-8', $output);
return $output;
}
/**
* 进行分词, 返回词语列表.
*/
function tokenize($str){
$tokens = array();
$output = self::cmd($input);
if($output){
$ps教程 = preg_split('/s+/', $output);
foreach($ps as $p){
list($seg, $tag) = explode('/', $p);
$item = array(
'seg' => $seg,
'tag' => $tag,
);
$tokens[] = $item;
}
}
return $tokens;
}
}
NLP::set_cmd_path(dirname(__FILE__));
?>

用起来很简单(确保 ICTCLAS 编译后的可执行文件和词典在当前目录):
复制代码 代码如下:
<?php
require_once('NLP.php');
var_dump(NLP::tokenize('Hello, World!'));
?>

进行中文分词的 PHP 类就在下面了, 用 proc_open() 函数来执行分词程序, 并通过管道和其交互, 输入要进行分词的文本, 读取分词结果。

 

[!--infotagslink--]

相关文章

  • php KindEditor文章内分页的实例方法

    我们这里介绍php与KindEditor编辑器使用时如何利用KindEditor编辑器的分页功能实现文章内容分页,KindEditor编辑器在我们点击分页时会插入代码,我们只要以它为分切符,就...2016-11-25
  • 自己动手写的jquery分页控件(非常简单实用)

    最近接了一个项目,其中有需求要用到jquery分页控件,上网也找到了需要分页控件,各种写法各种用法,都是很复杂,最终决定自己动手写一个jquery分页控件,全当是练练手了。写的不好,还请见谅,本分页控件在chrome测试过,其他的兼容性...2015-10-30
  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • jquery实现的伪分页效果代码

    本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • js识别uc浏览器的代码

    其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
  • vue.js 表格分页ajax 异步加载数据

    Vue.js通过简洁的API提供高效的数据绑定和灵活的组件系统.这篇文章主要介绍了vue.js 表格分页ajax 异步加载数据的相关资料,需要的朋友可以参考下...2016-10-20
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • php怎么用拼音 简单的php中文转拼音的实现代码

    小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
  • php导出csv格式数据并将数字转换成文本的思路以及代码分享

    php导出csv格式数据实现:先定义一个字符串 存储内容,例如 $exportdata = '规则111,规则222,审222,规222,服2222,规则1,规则2,规则3,匹配字符,设置时间,有效期'."/n";然后对需要保存csv的数组进行foreach循环,例如复制代...2014-06-07
  • ecshop商品无限级分类代码

    ecshop商品无限级分类代码 function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset($cat_options[$spec_cat_id]))...2016-11-25