首页 > 编程技术 > php

php无限级分类实例三

发布时间:2016-11-25 15:50

无限分灰一般都会用到递归来实现,下面我们来看看我提供的三个无限分类的读出方法。

数据库教程


--
-- Table structure for table `category`
--
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catpath` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;
--
-- Dumping data for table `category`
--
INSERT INTO `category` (`id`, `catpath`, `name`) VALUES
(1, '0', '网站首页'),
(2, '0-1', 'Linux OS'),
(3, '0-1', 'Apache服务器'),
(4, '0-1', 'MySQL数据库'),
(5, '0-1', 'PHP脚本语言'),
(6, '0-1-2', 'Linux 系统教程'),
(7, '0-1-2', 'Linux 网络技术'),
(8, '0-1-2', 'Linux 安全基础'),
(9, '0-1-2-7', 'Linux LAMP'),
(10, '0-1-3-10', 'apache Server');

php教程代码

$conn = mysql教程_connect ( 'localhost', 'root', '' );
mysql_select_db ( 'test', $conn );
mysql_query ( 'set names UTF8' );
$sql = "select id,concat(catpath,'-',id) as abspath,name from category order by abspath";
$query = mysql_query ( $sql );
while ( $row = mysql_fetch_array ( $query ) ) {
/**
* 第一种展示方法
*/
/*$space = str_repeat ( ' ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
echo $space . $row ['name'] . '<br>';*/
/**
* 第二种展示方法
*/
$space = str_repeat ( ' ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
$option .= '<option value="' . $row ['id'] . '">' . $space . $row ['name'] . '</option>';
}
echo '<select name="opt">' . $option . '</select>';

效果

过滤链接最简单的办法就是利用php教程的strip_tags函数,如下

php过滤html的函数:strip_tags(string) 这样就可以过滤掉所有的html标签了。
如果想过滤掉除了<img" width=100% src="">之外的所有html标签,则可以这样写:strip_tags(string,"<img>");
过滤除了<img" width=100% src=""><p>xxx</p><b></b>之外的所有html标签,

则可以这样写:

strip_tags(string,"<img><p><b>");

上面的做法不可取,因为它把所有的html都给过滤了,下面我们只要过滤连接部份。

<?php
echo preg_replace("/(?<=href=)([^>]*)(?=>)/i","#", "<a href='www.111cn.net'>你好,点这里看看</a><a href='www.111cn.net'>你好,点这里看看</a>");
?>
正则:/(?<=href=)([^>]*)(?=>)/
(?<=exp) 匹配exp后面的位置
(?=exp) 匹配exp前面的位置
此正则 匹配 在 href= 之后 “>” 之前 的 非 “>” 的所有字符
例子:<a href='www.111cn.net '>

升级一下,我们只过滤其它网站的链接保存自己网站的

 

$str ='<li><a href="http://www.111cn.net/12345s" target="_blank"> 施华洛世奇!</a></li><li><a href=http://123.com/n.php?a=luxury& target="_blank"> f乔</a></li><li><a href="http://mb.111cn.net/" target="_blank"> f衣';

$match='/<li><ashref="?http://[w.]*123.com/[w]+.php?a=*[w&=]*"?s+target="_blank">(.*)</a></li>/isu';
preg_match_all($match,$str,$arr);
echo "<pre>";
print_r($arr[0]);
echo "</pre>";

更多详细内容请查看:http://www.111cn.net/phper/php/35063.htm

开发中经常用到关于用程序 模仿用户post信息,下面我介介绍了几种方法,有需要的朋友参考一下。
# <?php教程  
# /** 
# * Socket版本 
# * 使用方法: 
# * $post_string = "app=socket&version=beta"; 
# * request_by_socket('facebook.cn','/restServer.php',$post_string); 
# */ 
# function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30){  
#     $socket = fsockopen($remote_server,$port,$errno,$errstr,$timeout);  
#     if (!$socket) die("$errstr($errno)");  
#      
#     fwrite($socket,"POST $remote_path HTTP/1.0");  
#     fwrite($socket,"User-Agent: Socket Example");  
#     fwrite($socket,"HOST: $remote_server");  
#     fwrite($socket,"Content-type: application/x-www-form-urlencoded");  
#     fwrite($socket,"Content-length: ".strlen($post_string)+8."");  
#     fwrite($socket,"Accept:*/*");  
#     fwrite($socket,"");  
#     fwrite($socket,"mypost=$post_string");  
#     fwrite($socket,"");  
#      
#     $header = "";  
#     while ($str = trim(fgets($socket,4096))) {  
#         $header.=$str;  
#     }  
#      
#     $data = "";  
#     while (!feof($socket)) {  
#         $data .= fgets($socket,4096);  
#     }  
#      
#     return $data;  
# }  
#  
关socket更详细教程请查看
http://www.111cn.net/phper/30/7cadb3c9195ac7d8ac9104da61a25c6e.htm

# /**  
# * Curl版本  
# * 使用方法:  
# * $post_string = "app=request&version=beta";  
# * request_by_curl('http://facebook.cn/restServer.php',$post_string);  
# */  
# function request_by_curl($remote_server,$post_string){  
#     $ch = curl_init();  
#     curl_setopt($ch,CURLOPT_URL,$remote_server);  
#     curl_setopt($ch,CURLOPT_POSTFIELDS,'mypost='.$post_string);  
#     curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);  
#     curl_setopt($ch,CURLOPT_USERAGENT,"Jimmy's CURL Example beta");  
#     $data = curl_exec($ch);  
#     curl_close($ch);  
#     return $data;  
# } 

cURL库可以简单和有效地去抓网页,您只需要运行一个脚本,然后分析一下您所抓取的网页,然后就可以以程序的方式得到您想要的数据了。无论是您想从一个链接上取部分数据,或是取一个XML文件并把其导入数据库教程,哪怕就是简单的获取网页内容,cURL是一个功能强大的PHP库。本文主要讲述如果使用这个PHP库。

curl参考文献

http://www.111cn.net/phper/18/curl-php.htm

 
# /** 
# * 其它版本 
# * 使用方法: 
# * $post_string = "app=request&version=beta"; 
# * request_by_other('http://facebook.cn/restServer.php',$post_string); 
# */ 
# function request_by_other($remote_server,$post_string){  
#     $context = array(  
#         'http'=>array( 
#             'method'=>'POST', 
#             'header'=>'Content-type: application/x-www-form-urlencoded'."". 
#                       'User-Agent : Jimmy's POST Example beta'."".  
#                       'Content-length: '.strlen($post_string)+8,  
#             'content'=>'mypost='.$post_string)  
#         );  
#     $stream_context = stream_context_create($context);  
#     $data = file_get_contents($remote_server,FALSE,$stream_context);  
#     return $data;  
# }  
#  
# ?>

file_get_contents() 函数把整个文件读入一个字符串中。

和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。

file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。

参考文献
http://www.111cn.net/phper/24/f8d52eaae81ea27383375ead36ffbd4d.htm

php教程显示文章 几分钟前,几小时前,几天前发布类

文章发表时的UNIX时间戳,来转化为例如 几分钟前,几小时前,几天前 这样的提示。如微博

 这看起来更加人性化


 1 <?php
 2  class timeAgo
 3  { 
 4     static $timeagoObject;  
 5     private $rustle;
 6     private $unit;
 7    
 8      private function __construct()
 9      {
10         
11      }               
12      private function __clone(){ }
13      public static function getObject()
14      {
15          if(! (self::$timeagoObject instanceof self) )
16                 self::$timeagoObject = new timeAgo();
17              
18          return self::$timeagoObject; 
19      }
20      private function count_int($unix_C)   // main function
21      {
22          if(! (isset($unix_C) || is_numeric($unix_C)) )
23              return 'don't find parameter';
24             
25          $d = time()-$unix_C ;   // $d - unix time difference value
26          $d_int =(int)floor($d/60) ; // minimum unit -- minutes   unix/60
27         
28          $this->unit = 0 ;   // is minutes,hour or day?
29         
30          if($d_int < 60){   // minutes   in one hour  3600
31             $this->rustle = $d_int;
32             $this->unit = 1; 
33             }
34            else if($d_int < 720){  //hour    in one day  3600*12
35                  $this->rustle = floor($d_int/60);
36                  $this->unit = 2 ;
37                  }  
38               else if($d_int < 7200){  //day  in ten days  3600*12*10
39                       $this->rustle = floor($d_int/720);
40                       $this->unit = 3 ;
41                       }
42                   else{
43                       $this->rustle = $d ;
44                       $this->unit = 4 ;  
45                       }
46      }
47      public function piece_str($C)
48      {
49           $this->count_int($C);
50             
51           $u = '';
52           switch( $this->unit )
53           {
54              case 1:
55                   $u = 'minute';
56                   break;
57              case 2:
58                   $u = 'hour';
59                   break;
60              case 3:
61                   $u = 'day';
62                   break;
63              case 4:
64                   $u = '';
65                   break;
66              case 0:
67                   return 'sorry , get time is fail';    
68           }
69           if($this->unit < 4)
70           {
71           if($this->rustle > 1)
72                return (string)$this->rustle.$u.'s ago';
73           else if($this->rustle == 1)
74                  return (string)$this->rustle.$u.'ago';
75               else
76                   return 'Just now'; 
77           }
78      }
79      /*  example:   $ago = timeAgo::getObject();
80       *             echo $ago->piece_str($unix);   
81       *             // 2 days ago
82      */
83  }
84 ?>

php教程 代码运行时间查看类

//date:2011-08-05
class RunTime//页面执行时间类
{
 private $starttime;//页面开始执行时间
 private $stoptime;//页面结束执行时间
 private $spendtime;//页面执行花费时间
 function getmicrotime()//获取返回当前微秒数的浮点数
 {
  list($usec,$sec)=explode(" ",microtime());
  return ((float)$usec + (float)$sec);
 }
 function start()//页面开始执行函数,返回开始页面执行的时间
 {
  $this->starttime=$this->getmicrotime();
 }
 function end()//显示页面执行的时间
 {
  $this->stoptime=$this->getmicrotime();
  $this->spendtime=$this->stoptime-$this->starttime;
  //return round($this->spendtime,10);
 }
 function display()
 {
     //$this->end();
     echo "<p>运行时间:".round($this->spendtime,10)."秒</p>";
 }
}
/*调用方法  
$timer=new Runtime();
$timer->start(); 

$timer->end();
view sourceprint?
$timer->display();

标签:[!--infotagslink--]

您可能感兴趣的文章: