最優(yōu)良人 » php http://www.dgkai.cn/blog 中山php|最優(yōu)網(wǎng)絡(luò) Mon, 13 May 2013 04:56:43 +0000 en hourly 1 http://wordpress.org/?v=3.1.4 php多維數(shù)組的搜索 http://www.dgkai.cn/blog/view-425.html http://www.dgkai.cn/blog/view-425.html#comments Mon, 17 Dec 2012 12:48:04 +0000 lin http://www.dgkai.cn/blog/?p=425 1 php搜索多維數(shù)組的鍵值

如下面例子:

$foo[1]['a']['xx'] = 'bar 1';
$foo[1]['b']['xx'] = 'bar 2';
$foo[2]['a']['bb'] = 'bar 3';
$foo[2]['a']['yy'] = 'bar 4';
$foo[3]['c']['dd'] = 'bar 3';
$foo[3]['f']['gg'] = 'bar 3';
$foo['info'][1] = 'bar 5';

如果要查找 bar 3 怎么進(jìn)行查找呢。有三個(gè)結(jié)果,而這三個(gè)結(jié)果都要,看下面的函數(shù):
-------------------------------------------------------------------------------------------------------------------------------
function array_search_re($needle, $haystack, $a=0, $nodes_temp=array()){
global $nodes_found;
$a++;
foreach ($haystack as $key1=>$value1) {
??? $nodes_temp[$a] = $key1;
??? if (is_array($value1)){???
????? array_search_re($needle, $value1, $a, $nodes_temp);
??? }
??? else if ($value1 === $needle){
????? $nodes_found[] = $nodes_temp;
??? }
}
return $nodes_found;
}
---------------------------------------------------------------------------------------------------------------------------------
這個(gè)函數(shù)就可以把上面要查找到的內(nèi)容全部返回出鍵名來
$result = array_search_re('bar 3', $foo);

print_r($result);

輸出結(jié)果為如下:
Array ( [0] => Array ( [1] => 2 [2] => a [3] => bb )
?????? ?? [1] => Array ( [1] => 3 [2] => c [3] => dd )
?????? ?? [2] => Array ( [1] => 3 [2] => f [3] => gg )
???? ?? )

1 php搜索多維數(shù)組的鍵名

function array_search_key($needle, $haystack){
global $nodes_found;

foreach ($haystack as $key1=>$value1) {
?
?if ($key1=== $needle){
?
??$nodes_found[] = $value1;
???????
?? }
??? if (is_array($value1)){???
????? array_search_key($needle, $value1);
??? }
???
???
}

return $nodes_found;
}
$result = array_search_key('a', $foo);

print_r($result);

輸出結(jié)果為如下:
?

Array
(
??? [0] => Array
??????? (
??????????? [xx] => bar 1
??????? )

??? [1] => Array
??????? (
??????????? [bb] => bar 3
??????? )

??? [2] => Array
??????? (
??????????? [yy] => bar 4
??????? )

)

]]>
http://www.dgkai.cn/blog/view-425.html/feed 743
php過濾客戶提交參數(shù),防注入 http://www.dgkai.cn/blog/view-417.html http://www.dgkai.cn/blog/view-417.html#comments Sat, 24 Nov 2012 09:16:40 +0000 lin http://www.dgkai.cn/blog/?p=417 以下代碼實(shí)現(xiàn)過濾php的$_GET 和$_POST參數(shù)

/**
* 安全防范
*/
function Add_S($array)
{
foreach($array as $key=>$value)
{
if(!is_array($value))
{
$value = get_magic_quotes_gpc()?$value:addslashes($value);
$array[$key]=filterHtml($value);
}
Else
{
Add_S($array[$key]);
}
}
return $array;
}
function glstr($var) {

if (is_array($var)) {
return Add_S($var);
}
elseif(strlen($var)){
$var = get_magic_quotes_gpc()?$var:addslashes($var);

$var = filterHtml($var);
}
return $var;
}
function filterHtml($html)
{
$farr = array(
"/<!DOCTYPE([^>]*?)>/eis",
"/<(\/?)(html|body|head|link|meta|base|input)([^>]*?)>/eis",
"/<(script|i?frame|style|title|form)(.*?)<\/\\1>/eis",
"/(<[^>]*?\s+)on[a-z]+\s*?=(\"|')([^\\2]*)\\2([^>]*?>)/isU",//過濾javascript的on事件
"/\s+/",//過濾多余的空白
);
$tarr = array(
"",
"",
"",
"\\1\\4",
" ",
);
$html = preg_replace( $farr,$tarr,$html);
return $html;
}
if (sizeof($_GET)) {
foreach($_GET as $key => $value) {
$_GET[$key] = glstr($value); //
}

}
if (sizeof($_POST)) {
foreach($_POST as $key => $value) {
$_POST[$key] = glstr($value); //
}
}

]]>
http://www.dgkai.cn/blog/view-417.html/feed 343
smarty模版使用php標(biāo)簽,如何獲取模版變量 http://www.dgkai.cn/blog/view-409.html http://www.dgkai.cn/blog/view-409.html#comments Sat, 22 Sep 2012 03:54:23 +0000 lin http://www.dgkai.cn/blog/?p=409 已經(jīng)assign一個(gè)模版變量$assign,由于要做特殊的循環(huán)輸出,使用for循環(huán),因此使用到了php標(biāo)簽,但是php語句和模版語句的變量作用域是不同的,因此不能直接獲取到

{{php}}

for($i=0;$i<count($assign);$i=$i+2){
echo '
<ul>
<li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title="">'.$assign[$i][title].'</a></span> </li>
<li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i+1][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title="">'.$assign[$i+1][title].'</a></span> </li>i>

</ul>';}
{{/php}}

解決的方法是:模版變量全部存在smarty的一個(gè)對(duì)象里面;只要在for之前進(jìn)行賦值:$assign = $this->_tpl_vars[assign];

{{php}}
$assign = $this->_tpl_vars[assign];
for($i=0;$i<count($assign);$i=$i+2){
echo '
<ul>
<li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title="">'.$assign[$i][title].'</a></span> </li>
<li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i+1][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title="">'.$assign[$i+1][title].'</a></span> </li>i>

</ul>';}
{{/php}}

]]>
http://www.dgkai.cn/blog/view-409.html/feed 502
php字符串截取函數(shù),自動(dòng)清除網(wǎng)頁標(biāo)簽 http://www.dgkai.cn/blog/view-382.html http://www.dgkai.cn/blog/view-382.html#comments Thu, 07 Jun 2012 09:40:29 +0000 lin http://www.dgkai.cn/blog/?p=382 /**
* 字符串截取函數(shù) 自動(dòng)清除網(wǎng)頁標(biāo)簽
**/
Function str_cut($string, $length = 80, $etc = '...', $code = 'UTF-8')
{
$string = strip_tags(preg_replace('!\s+!', $string, ' '));
if ($length == 0)
return '';
if ($code == 'UTF-8') {
$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
}
else {
$pa = "/[\x01-\x7f]|[\xa1-\xff][\xa1-\xff]/";
}
preg_match_all($pa, $string, $t_string);
if (count($t_string[0]) > $length)
return join('', array_slice($t_string[0], 0, $length)) . $etc;
return join('', array_slice($t_string[0], 0, $length));
}

]]>
http://www.dgkai.cn/blog/view-382.html/feed 4
php清空某個(gè)目錄的方法 http://www.dgkai.cn/blog/view-378.html http://www.dgkai.cn/blog/view-378.html#comments Mon, 04 Jun 2012 07:05:50 +0000 lin http://www.dgkai.cn/blog/?p=378 以下代碼可以清空多層的目錄結(jié)構(gòu):

function del_dir($dir){
{
if (!$dir) { return ; }
$cacheDir = $dir;
$dh = opendir($cacheDir);
while ( $file = readdir($dh) ) {

if (($file == '.') || ($file == '..')) { continue; }

if (file_exists( $cacheDir .'/'.$file)) {
if(is_dir( $cacheDir .'/'.$file)){
del_dir($cacheDir .'/'.$file);
}elseif (!unlink($cacheDir .'/'. $file)) {

//刪除完操作
}
}
}

}
}

]]>
http://www.dgkai.cn/blog/view-378.html/feed 82
phpmyadmin導(dǎo)出的sql如何通過php導(dǎo)入 http://www.dgkai.cn/blog/view-374.html http://www.dgkai.cn/blog/view-374.html#comments Mon, 04 Jun 2012 04:15:01 +0000 lin http://www.dgkai.cn/blog/?p=374 以下函數(shù)可以直接把從phpmyadmin導(dǎo)出的sql文件直接通過php插入數(shù)據(jù)庫:

//$file表示通過讀取sql文件返回的字符串,比如$file2=file_get_contents('db.sql');

function import($file2){

$Db = new Db();
$file2=explode("\n",$file2);//將文件內(nèi)容按行讀入到數(shù)組

$c1=count($file2);
for($j=0;$j<$c1;$j++)
{
$ck=substr($file2[$j],0,4);//取每行的前4個(gè)字符
if( ereg("#",$ck)||ereg("--",$ck) )//去掉注釋
{
continue;
}
$arr[]=$file2[$j];//將去掉注釋的文件內(nèi)容按行讀入數(shù)組$arr,數(shù)組每個(gè)元素對(duì)應(yīng)一行
}
$read=implode("\n",$arr); //重新組織文件內(nèi)容到一個(gè)字符串,(按照原來分好的一行一行的)
$sql=str_replace("\r",'',$read);//去掉"\r(回車符)"

$detail=explode(";\n",$sql);
//將經(jīng)上述整理過的文件內(nèi)容再次按一條完整的sql語句(以;和\n分隔)導(dǎo)入到數(shù)組$detail,
//此時(shí)數(shù)組detail的每個(gè)元素對(duì)應(yīng)一條完整的sql語句
$count=count($detail);
for($i=0;$i<$count;$i++)
{
$sql=str_replace("\r",'',$detail[$i]);//去掉每行sql中的回車符
$sql=str_replace("\n",'',$sql);//去掉換行符
$sql=trim($sql);//去掉前后空格
//現(xiàn)在的$sql
$Db->query($sql);

}

}

]]>
http://www.dgkai.cn/blog/view-374.html/feed 21
php date函數(shù)的時(shí)區(qū)問題,輸出時(shí)間有誤 http://www.dgkai.cn/blog/view-372.html http://www.dgkai.cn/blog/view-372.html#comments Sat, 02 Jun 2012 02:16:24 +0000 lin http://www.dgkai.cn/blog/?p=372 問題:echo date('o-m-j H:i'); 輸出結(jié)果與本地時(shí)間相差8小時(shí)。
原因:時(shí)區(qū)未正確設(shè)置。
兩種解決方法:

1.加入date_default_timezone_set('Asia/Shanghai');
2. 修改php.ini
[Date]
; Defines the default timezone used by the date functions
date.timezone = PRC 注:PRC為中化人民共和國(guó)

]]>
http://www.dgkai.cn/blog/view-372.html/feed 15
php使用mail函數(shù)發(fā)送郵件,解決亂碼問題 http://www.dgkai.cn/blog/view-370.html http://www.dgkai.cn/blog/view-370.html#comments Fri, 01 Jun 2012 03:03:27 +0000 lin http://www.dgkai.cn/blog/?p=370 $to = 'sales@zui88.com';
$subject = "=?UTF-8?B?".base64_encode('網(wǎng)站收到一個(gè)新信息')."?=";
$message = "
用戶姓名:{$_POST['name']}\n
用戶郵箱:{$_POST['email']}\n
用戶電話:{$_POST['contact']}\n
留言內(nèi)容:{$_POST['content']}
";
$headers = 'From: webmaster@zui88.com' . "\r\n" .
'Reply-To: webmaster@zui88.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

mail($to, $subject, $message, $headers);

]]>
http://www.dgkai.cn/blog/view-370.html/feed 24
移植方便的php數(shù)據(jù)庫操作類,建表增刪改查 http://www.dgkai.cn/blog/view-368.html http://www.dgkai.cn/blog/view-368.html#comments Wed, 30 May 2012 09:46:53 +0000 lin http://www.dgkai.cn/blog/?p=368 <?php
/**
*
* 數(shù)據(jù)操作類
* @author Xiaoqiang
* @link //
* @copyright Xiaoqiang
* @version Svn $Id$
* @package
*/
class Db {

var $links;
// 構(gòu)造函數(shù),初始化數(shù)據(jù)庫(主機(jī),用戶名,密碼,數(shù)據(jù)庫名)
function __construct() {

$this->links = mysql_connect(DB_HOST, DB_USER, DB_PWD);
if(DEBUG){
mysql_select_db(DB_NAME) or die('ERROR:'.mysql_error());
}
else{
mysql_select_db(DB_NAME);
}
$char_sql = "SET NAMES '" . CHARSET . "'";
$this->query($char_sql);
}

/**
* ****************** 操作 ******************
*/
/**
* 增刪改查操作方法
* 輸入sql語句
* 返回布爾值或結(jié)果集$row
*/
function query($sql) {
if(DEBUG){
$render = mysql_query($sql) or die('query ERROR:'.mysql_error()."<br>sql語句出錯(cuò):" . $sql);
return $render;
}
else{
return mysql_query($sql);
}
}
/**
* 計(jì)算行數(shù)方法
* 輸入
* 結(jié)果數(shù)組
*/
function count($table,$condition='1') {

$sql = "select count(*) from `".DB_PREFIX.$table."` where $condition";
$result = $this->select($sql);
return $result[0]['count(*)'];
}
/**
* 原始的sql語句查操作方法
* 輸入sql語句
* 結(jié)果數(shù)組
*/
function select($sql) {
$row = $this->query($sql);
$results = array();
while($arr = $this->fetch($row)){
$results[] = $arr;
}
//$this->free_result($row);
return $results;
}

/**
* 檢查某字段是否存在某值
* @param 輸入表名.字段,值
* @return id 或者 false
**/
function check_exists($table,$val){
$render = false;
$tab = explode('.',$table);
if($tab['1'] && $tab['1']!='id'){
$fields = $tab['1'];
$table = "{$tab[0]}.id,{$fields}";
}
else{
$fields = 'id';
$table = $tab[0].".id";
}
$condition = "`$fields` = '{$val}'";
$detail = $this->read($table,$condition);
if($detail[$fields]){
$render = $detail['id'];
}
return $render;
}

/**
* 查詢多條數(shù)據(jù)方法
* 輸入表名.字段,字段;查詢條件,條數(shù)
* 如果條件是數(shù)組,則進(jìn)入高級(jí)搜索模式
* 返回結(jié)果數(shù)組
*/
function readall($table,$condition='1',$limit='') {
$tab = explode('.',$table);
$table = $tab['0'];
if($tab['1']){
$fields = $tab['1'];
$fields_array =explode(',',$fields);
$fields = '';
foreach( $fields_array as $one){
$fields .= "`$one`,";
}
$fields = rtrim($fields,',');
}
else{
$fields = '*';
}
if(is_array($condition)){
$condition = $this->parse_condition($condition);
}
$sql = "select $fields from `".DB_PREFIX.$table."` where $condition";
if($limit)$sql .= " limit $limit";
return $this->select($sql);
}
/**
* 查詢單條數(shù)據(jù)方法
* 輸入表名.字段,字段;查詢條件
* 返回結(jié)果數(shù)組
*/
function read($table,$condition='1') {
$render = $this ->readall($table,$condition,1);
return $render[0];
}
/**
* 修改數(shù)據(jù)方法
* 輸入表名,插入數(shù)據(jù)array('字段'=>'值'),條件
* 返回布爾值
*/
function update($table,$data,$condition ) {

$set = '';
foreach( $data as $key=>$val){
$set .= "`$key` = '".$val."',";
}
$set = rtrim($set,',');
if(is_array($condition)){
$condition = $this->parse_condition($condition);
}
$sql = "update `".DB_PREFIX.$table."` set $set where $condition";
return $this->query($sql);
}
/**
* 插入數(shù)據(jù)方法
* 輸入表名,數(shù)據(jù)array('字段'=>'值')
* 返回布爾
*/
function insert($table,$data) {

$fields = array();
$values = array();

foreach( $data as $key=> $val){
if(is_array($val)){
$_values = array();
$_fields = array();
foreach( $val as $k=> $v){
$_fields[]= "`$k`";
$_values[]= "'{$v}'";
}
$fields = $_fields;
$values[] = '('.implode(',',$_values).')';
}
else{
$fields[] = "`$key`";
$values[] = "'{$val}'";
}
}
$fields = implode(',',$fields);
$values = implode(',',$values);
$sql = "insert into `".DB_PREFIX.$table."` ($fields) values($values)";
return $this->query($sql);
}
/**
* 刪除數(shù)據(jù)方法
* 輸入表名,條件
* 返回bool
*/
function delete($table,$condition) {

if(empty($condition)){
die('條件不能為空');
}
if(is_array($condition)){
$condition = $this->parse_condition($condition);
}
$sql = "delete from `".DB_PREFIX.$table."` where $condition";
return $this->query($sql);
}
/**
* 解析條件的函數(shù)
* @param 條件數(shù)組
* <code>
$arr[] = "`id`==0";
$arr[] = "`id`==5";
$arr['id'] = "5";
$arr['or'][] = "`id`!=2";
$arr['or'][] = "`id`!=1";
$arr['or'][] = "`id`!=2";
$arr['groups'][]='id';
$arr['orders']['id']='asc';
$arr['orders']['td']='DESC';
* </code>
* @return str
**/
function parse_condition($condition){
$and = '1';
$or = '0';
$groups = array();
$orders = array();
foreach( $condition as $key=>$val){
if(is_numeric($key)){
$and .= " and $val";
}
elseif(strtolower($key)== 'or'){
//處理or條件
if(is_array($val)){
foreach( $val as $k=>$v){
if(is_numeric($k)){
$or .= " or {$v}";
}
elseif(is_array($v)){
$v = implode(',',$v);
$or .= " or `$k` in ($v)";
}else{
$or .= " or `$k='{$v}'";
}
}

}else{
$or .= " or $val'";
}
}
elseif(strtolower($key)== 'groups'){
//處理group by
foreach( $val as $k=>$v){
$groups[] = $v;
}

}elseif(strtolower($key)== 'orders'){
//處理order by
foreach( $val as $k=>$v){
$orders[] = $k.' '.$v;
}

}else{
if(is_array($val)){
$val = implode(',',$val);
$and .= " and `$key` in ($val)";
}else{
$and .= " and `$key`='{$val}'";
}
}
}
if($and!='1' && $or!='0')$where = $and.' or '.$or;
elseif($and!='1') $where = $and;
elseif($or!='0') $where = $or;
if($groups)$where .= " group by ".implode(',',$groups);
if($orders)$where .= " order by ".implode(',',$orders);
$where = str_replace('1 and','',str_replace('0 or','',$where));

return $where;
}
/**
* 鎖表方法
* 輸入表名,鎖定類型,r or w 寫鎖要放在讀鎖前面
* 返回bool
*/
function lock($table,$type='r') {

if($type=='r'){
$type = 'READ';
}
else{
$type = 'WRITE';
}
$sql = "lock table `".DB_PREFIX.$table."` $type";
return $this->query($sql);
}
/**
* 解鎖表方法
*
* 返回bool
*/
function unlock( ) {

$sql = "unlock tables";
return $this->query($sql);
}
/**
* 結(jié)果集放入數(shù)組方法
* 返回?cái)?shù)組,指針下移
*/

function fetch($row) {

return mysql_fetch_array($row,MYSQL_ASSOC);

}

/**
* 計(jì)算結(jié)果集行數(shù)方法
* 輸入$row
* 返回行數(shù)
*/

function num_rows($row) {

return mysql_num_rows($row);

}

/**
* 計(jì)算結(jié)果集列數(shù)方法
* 輸入$row
* 返回列數(shù)
*/

function num_fields($row) {

return mysql_num_fields($row);

}

/**
* 釋放result結(jié)果集內(nèi)存
* 返回布爾值
*/

function free_result($row) {

return mysql_free_result($row);

}

/**
* 查看指定表的字段名
* 輸入表名
* 返回全部字段名數(shù)組
*/
function list_fields($table) {
$fields = mysql_list_fields(DB_NAME, DB_PREFIX.$table, $this->links);
$columns = mysql_num_fields($fields);

for ($i = 0; $i < $columns; $i++) {
$row[$i] = mysql_field_name($fields, $i);
}
return $row;

}

/**
* 查看數(shù)據(jù)庫版本方法
*/

function version() {
return mysql_get_server_info();

}

/**
* 插入時(shí)查看插入ID
*/

function insert_id() {
return mysql_insert_id();

}
/**
* 分頁方法
*/

function page($table,$condition='1',$pagesize=20,$id='page') {
$page = $_GET[$id];
if(!$page)$page= 0 ;
elseif(!is_numeric($page))die('分頁出錯(cuò)');

//查找結(jié)果集
$p = $page * $pagesize;
$limit = $p.",".$pagesize;
$results = $this->readall($table,$condition,$limit);

//取得結(jié)果集行數(shù)
$num = $this->count($table,$condition);
//定義最后頁 $maxpage
if ($num % $pagesize) {
$maxpage = (int) ($num / $pagesize +1);
} else
$maxpage = $num / $pagesize;
if(STATICS){
//從服務(wù)器端取得url信息

if($_GET[$id] === null){
$_SERVER["REQUEST_URI"] = str_replace('index.php','',$_SERVER["REQUEST_URI"]);
$_SESSION[$id] = str_replace('.html','',$_SERVER["REQUEST_URI"],$count);
$_SESSION[$id] = $count?$_SESSION[$id] :$_SESSION[$id].'index';
if(!sizeof($_GET))$_SESSION[$id].="-htm";
}
$str = "<div class=pg style=padding-top:25px;><a href=$_SESSION[$id]-".$id."-0.html>First</a> ";
if($page)$str .= "<a href=$_SESSION[$id]-".$id."-".($page-1).".html>Previous</a> ";
if($page-3>=0)$str .="<a href=$_SESSION[$id]-".$id."-".($page-3).".html>".($page-2)."</a> ";
if($page-2>=0)$str .="<a href=$_SESSION[$id]-".$id."-".($page-2).".html>".($page-1)."</a> ";
if($page-1>=0)$str .="<a href=$_SESSION[$id]-".$id."-".($page-1).".html>".$page."</a> ";
if($page < $maxpage)$str .=($page+1)." ";
if($page+1 < $maxpage)$str .="<a href=$_SESSION[$id]-".$id."-".($page+1).".html>".($page+2)."</a> ";
if($page+2 < $maxpage)$str .="<a href=$_SESSION[$id]-".$id."-".($page+2).".html>".($page+3)."</a> ";
if($page+3 < $maxpage)$str .="<a href=$_SESSION[$id]-".$id."-".($page+3).".html>".($page+4)."</a> ";
if($page+1 < $maxpage)$str .="<a href=$_SESSION[$id]-".$id."-".($page+1).".html>Next</a> ";
if(!$maxpage)$maxpage=1;
$str .="<a href=$_SESSION[$id]-".$id."-".($maxpage-1).".html>Last</a> <a href=javascript:;> ".($page+1)."/".$maxpage."Total</a> </div>";
}
else{
//從服務(wù)器端取得url信息

if($_GET[$id] === null){
$_SESSION[$id] = $_SERVER["REQUEST_URI"];
if(!sizeof($_GET))$_SESSION[$id].="?p=1";

}

$str = "<div class=pg style=padding-top:25px;><a href=$_SESSION[$id]&".$id."=0>First</a> ";
if($page)$str .= "<a href=$_SESSION[$id]&".$id."=".($page-1).">Previous</a> ";
if($page-3>=0)$str .="<a href=$_SESSION[$id]&".$id."=".($page-3).">".($page-2)."</a> ";
if($page-2>=0)$str .="<a href=$_SESSION[$id]&".$id."=".($page-2).">".($page-1)."</a> ";
if($page-1>=0)$str .="<a href=$_SESSION[$id]&".$id."=".($page-1).">".$page."</a> ";
if($page < $maxpage)$str .=($page+1)." ";
if($page+1 < $maxpage)$str .="<a href=$_SESSION[$id]&".$id."=".($page+1).">".($page+2)."</a> ";
if($page+2 < $maxpage)$str .="<a href=$_SESSION[$id]&".$id."=".($page+2).">".($page+3)."</a> ";
if($page+3 < $maxpage)$str .="<a href=$_SESSION[$id]&".$id."=".($page+3).">".($page+4)."</a> ";
if($page+1 < $maxpage)$str .="<a href=$_SESSION[$id]&".$id."=".($page+1).">Next</a> ";
if(!$maxpage)$maxpage=1;
$str .="<a href=$_SESSION[$id]&".$id."=".($maxpage-1).">Last</a> <a href=javascript:;> ".($page+1)."/".$maxpage."Total</a> </div>";
}
return array($results,$str);
}

/**
* 左連接多表查詢
* @param
* @return
**/
function leftjoin($left,$right,$on,$condition,$limit=1){
$left = explode('.',$left);
$right = explode('.',$right);
$left['0'] = "`".DB_PREFIX.$left['0']."`";
$right['0'] = "`".DB_PREFIX.$right['0']."`";
if($left['1'] || $right['1']){
$fields = '';
if(!empty($left['1'])){
$_field = explode(',',$left['1']);
foreach( $_field as $one){
$fields.=$left['0'].'.`'.$one."`,";
}
}
if(!empty($right['1'])){
$_field = explode(',',$right['1']);
foreach( $_field as $one){
$fields.=$right['0'].".`".$one."`,";
}
}
$fields = rtrim($fields,',');
}
else{
$fields = '*';
}
$on = str_replace('\2',$right[0],str_replace('\1',$left[0],$on));
$condition = str_replace('\2',$right[0],str_replace('\1',$left[0],$condition));
$sql = "SELECT {$fields} FROM {$left[0]} LEFT JOIN {$right[0]} ON ( {$on} ) WHERE ( {$condition} ) LIMIT {$limit} ";

$query = $this->query($sql);
$field_num = mysql_num_fields($query);
while($arr = mysql_fetch_array($query,MYSQL_NUM)){
$_arr = array();
for( $i=0 ; $i<$field_num ; $i++ ){
$table = str_replace(DB_PREFIX,'',mysql_field_table($query, $i));
$field = mysql_field_name($query, $i);
$_arr[$table.'.'.$field] = $arr[$i];
}
$array[]=$_arr;

}
$array=$limit==1?$arrat[0]:$array;
return $array;
}
/**
* 用于創(chuàng)建一個(gè)表結(jié)構(gòu)
* @param 表名,結(jié)構(gòu) array(字段,格式,注釋) 表注釋 索引array(字段,字段) 全文搜索(字段,字段)
* @return 打印
**/

function createTable($tablename,$data,$comment='',$key='',$fulltext=''){
$_key='';
$_type = '';
$_fulltext = '';
$tablename = DB_PREFIX.$tablename;
$sql = "CREATE TABLE IF NOT EXISTS `$tablename` (
`id` int(10) unsigned NOT NULL auto_increment COMMENT 'ID',
";
foreach( $data as $one){
switch($one[1]){
case '':
$_type = 'varchar(255)';
break;
case 'tinyint':
$_type = 'tinyint(1)';
break;
case 'time':
$_type = 'int(10)';
break;
default:

if(strpos($one[1],'.')!==false){
$_type = explode('.',$one[1]);
$_type = $_type[0].'('.$_type[1].')';
}
else{
$_type = $one[1];
}
break;
}
$sql.="`{$one[0]}` $_type NOT NULL COMMENT '{$one[2]}',
";
}

if(!empty($key)){
foreach( $key as $one){
$_key.="KEY `$one` (`$one`),
";
}
}
if(!empty($fulltext)){
foreach( $fulltext as $one){
$_key.="FULLTEXT `$one` (`$one`),
";
}
}

$sql.= $_key.$_fulltext."PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=".CHARSET." COMMENT '$comment';
";
if(!$_GET[sure]){

if(empty($_GET)){
$url = '?sure=1';

}
else{
$url = $_SERVER["REQUEST_URI"].'&sure=1';
}

echo "即將執(zhí)行以下建表操作:<br><pre>$sql<a href=$url>確定</a></pre>";
}
elseif($_GET[sure] && $this->query($sql)){

echo "完成操作";
}
else{
echo "操作失敗:<br>><pre>$sql</pre>";
}

exit;

}
};

//實(shí)例

define("DB_HOST", 'localhost');
define("DB_USER", 'root');
define("DB_PWD", '');
define("DB_NAME", "test");
define("DB_PREFIX", "test_");
define('CHARSET', 'gbk'); // 編碼
define("GEBUG", 1);

//建表
$Db=new Db();
$data[] = array('title','','標(biāo)題');
$data[] = array('open','tinyint.1','是否公開');
$data[] = array('keyword','','關(guān)鍵詞');
$data[] = array('content','text','內(nèi)容');
$data[] = array('created','time','時(shí)間');
//$Db->createTable('article',$data,'文章表');
//增刪改查
$data['title']='t';
$data['keyword']='k';
$Db->insert('article',$data);
$num = $Db->read('article.id','1 order by id desc');
$data['created'] = mktime()+$num['id'];
$Db->update('article',$data,"`id` =2");
$Db->delete('article',"`id` =3");

?>

]]>
http://www.dgkai.cn/blog/view-368.html/feed 3
php采集程序,提取網(wǎng)頁超鏈接,郵箱或其他特定內(nèi)容 http://www.dgkai.cn/blog/view-366.html http://www.dgkai.cn/blog/view-366.html#comments Wed, 30 May 2012 08:48:29 +0000 lin http://www.dgkai.cn/blog/?p=366 以下代碼從上一篇文章修改而來,專門用于提取網(wǎng)頁所有超鏈接,郵箱或其他特定內(nèi)容

<?php

function fetch_urlpage_contents($url){
$c=file_get_contents($url);
return $c;
}
//獲取匹配內(nèi)容
function fetch_match_contents($begin,$end,$c)
{
$begin=change_match_string($begin);
$end=change_match_string($end);
$p = "#{$begin}(.*){$end}#iU";//i表示忽略大小寫,U禁止貪婪匹配
if(preg_match_all($p,$c,$rs))
{
return $rs;}
else { return "";}
}//轉(zhuǎn)義正則表達(dá)式字符串
function change_match_string($str){
//注意,以下只是簡(jiǎn)單轉(zhuǎn)義
$old=array("/","$",'?');
$new=array("\/","\$",'\?');
$str=str_replace($old,$new,$str);
return $str;
}

//采集網(wǎng)頁
function pick($url,$ft,$th)
{
$c=fetch_urlpage_contents($url);
foreach($ft as $key => $value)
{
$rs[$key]=fetch_match_contents($value["begin"],$value["end"],$c);
if(is_array($th[$key]))
{ foreach($th[$key] as $old => $new)
{
$rs[$key]=str_replace($old,$new,$rs[$key]);
}
}
}
return $rs;
}

$url="http://www.dgkai.cn"; //要采集的地址
$ft["a"]["begin"]='<a'; //截取的開始點(diǎn)<br />
$ft["a"]["end"]='>'; //截取的結(jié)束點(diǎn)

$rs=pick($url,$ft,$th); //開始采集

print_r($rs["a"]);

?>

]]>
http://www.dgkai.cn/blog/view-366.html/feed 190
欧美日韩国产三级| 精品欧美久久| 五月 丁香 综合| 久久久久亚洲AV无码电影| xj99| 亚洲人妻中文字幕视频| 麻豆av| 另类国产| a视频在线| 亚欧洲精品在线视频免费观看| 日韩片| 亚洲AV中文无码乱人伦| 欧美成一色A一色好看| 国产91小视频| 久久户外偷拍| 男女搞| www.午夜| 亚洲日产中文字幕无码| 婷婷成人在线| 久久久久久久久98| 六月丁香七月婷婷| 安陆市| 核1024无码| 亚洲第一成人免费av| 久久天堂av综合合色| 亚洲熟妇无码久久精品| 国产黄a三级三级三级| 红杏成人区一区二区三区 | 欧美精品一| 国产精品乱码毛片在线看| 国产亚洲日韩欧美综合| 经典一区二区| 日韩呦呦在线| 麻豆91精品91久久久| 色婷婷久久久SWAG精品| 精品色呦呦| 1000篇欧美大视频| 涩亚洲精品| 欧美综合二区| 久久久久久久国产| 久久精品人妻少妇一区二区|