WelcomeYou :
感谢光临 DH`s BLOG 。欢迎常来踩!
php 二分法
作者:DH 日期:2009-12-03 13:08
CODE:
<?php
//search函数 其中$array为数组,$k为要找的值,$low为查找范围的最小键值,$high为查找范围的最大键值
function search($array, $k, $low=0, $high=0)
{
if(count($array)!=0 and $high == 0) //判断是否为第一次调用
{
$high = count($array);
}
if($low <= $high) //如果还存在剩余的数组元素
{
$mid = intval(($low+$high)/2); //取$low和$high的中间值
if ($array[$mid] == $k) //如果找到则返回
{
return $mid;
}
elseif ($k < $array[$mid]) //如果没有找到,则继续查找
{
return search($array, $k, $low, $mid-1);
}
else
{
return search($array, $k, $mid+1, $high);
}
}
return -1;
}
$array = array(4,5,7,8,9,10); //测试search函数
echo search($array, 8); //调用search函数并输出查找结果
?>
实例(Smarty+FCKeditor新闻系统)
作者:DH 日期:2009-10-13 22:42
无意间在网上搜到的没觉得还好,为了方便就记这了
实例(Smarty+FCKeditor新闻系统)
——一牛人学习php一个月的作业——
以下是主文件index.php的内容:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
require('./global.php');
require('./smarty/libs/Smarty.class.php');
require('./mysql.php');
require('./FCKeditor/fckeditor.php');
$action=$_REQUEST['action'];
//定义一个函数用于调用FCK
function editor($input_name, $input_value)
{
global $smarty;
$editor = new FCKeditor($input_name) ;
$editor->BasePath = "./FCKeditor/";//指定编辑器路径
$editor->ToolbarSet = "Default";//编辑器工具栏有Basic(基本工具),Default(所有工具)选择
$editor->Width = "100%";
$editor->Height = "320";
$editor->Value = $input_value;
$editor->Config['AutoDetectLanguage'] = true ;
$editor->Config['DefaultLanguage'] = 'en' ;//语言
$FCKeditor = $editor->CreateHtml();
$smarty->assign("editor", $FCKeditor);//指定区域
}
switch ($action){
case 'addnewsview':
$smarty= new Smarty();
$smarty->template_dir = './template';
$smarty->compile_dir = './smarty/templates_c';
$smarty->assign('page_title','新建新闻');
$smarty->assign('actionvalue','addnews');
editor('content','');//调用编辑器,并定义文本域名为content(与下面addnews中的$_REQUEST['content']对应
$smarty->display('addnews.htm');
break;
case 'addnews':
$title=$_REQUEST['title'];
$content=$_REQUEST['content'];
$db=new mysql();
$button=$_REQUEST['Submit'];
if(empty($title) || empty($content)){
echo "请填写完成!<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=./index.php?action=addnewsview\">";
}else{
$sql="insert into news values(id,'admin','$title','$content',NOW())";
$db->query_exec($sql);
echo "操作成功!<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=./index.php\">";
}
break;
case 'editnewsview':
$smarty= new Smarty();
$smarty->template_dir = './template';
$smarty->compile_dir = './smarty/templates_c';
$smarty->assign('page_title','修改新闻');
$smarty->assign('actionvalue','addnews');
$id=$_REQUEST['id'];
$query="select * from news where id=$id";
$db=new mysql();
$result = $db->query_exec($query);
$rs = $result-> fetch_assoc();
$smarty->assign('title',$rs['title']);
//$smarty->assign('content',$rs['content']);
$smarty->assign('actionvalue','editnews');
$smarty->assign('id',$rs['id']);
editor('content',$rs['content']);
$smarty->display('addnews.htm');
break;
case 'editnews':
$title=$_REQUEST['title'];
$content=$_REQUEST['content'];
$id=$_REQUEST['id'];
$button=$_REQUEST['Submit'];
$db=new mysql();
if ($button=='提交'){
$sql="update news set title='$title',content='$content',date=NOW() where id=$id";
$db->query_exec($sql);
echo "操作成功!<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=./index.php\">";
}
break;
case 'delnews':
$db=new mysql();
if ($checkbox!="" or count($checkbox)!=0) {
for ($i=0;$i<count($checkbox);$i++){
$db->query_exec("delete from news where id='$checkbox[$i]'");
}
}
echo "操作成功!<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=./index.php\">";
break;
default:
$smarty= new Smarty();
$smarty->template_dir = './template';
$smarty->compile_dir = './smarty/templates_c';
$smarty->assign('page_title','新闻管理');
$smarty->assign('actionvalue','delnews');
$query="select * from news";
$db=new mysql();
$result = $db->query_exec($query);
while ($rs = $result-> fetch_assoc()) {
$array[]= array("id"=>$rs['id'], "title"=>$rs['title'],"date"=>$rs['date']);
$smarty->assign('news',$array);
}
$smarty->display('index.htm');
}
?>
以下是模板文件index.htm的内容
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>{$page_title}</title>
</head>
<body>
<p class="style1">新闻管理</p>
<hr>
<table width="771" height="115" border="0">
<tr>
<td height="62"><div align="center">系统管理</div></td>
<td width="666" rowspan="2"><form name="form1" method="post" action="">
<table width="543" border="0">
<tr>
<td width="253">标题</td>
<td width="230">日期</td>
<td width="46">选择</td>
</tr>
{section name=news loop=$news}
<tr>
<td><a href="./index.php?action=editnewsview&id={$news[news].id}">{$news[news].title}</a></td>
<td>{$news[news].date}</td>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="{$news[news].id}"></td>
</tr>
{/section}
</table>
<p>
<input type="submit" name="Submit" value="删除">
<input name="action" type="hidden" id="action" value="{$actionvalue}">
</p>
</form> </td>
</tr>
<tr>
<td width="95" height="47"><div align="center"><a href="./index.php?action=addnewsview">添加新闻</a></div></td>
</tr>
</table>
<p class="style1"> </p>
</body>
</html>
以下是添加新闻的模板文件addnews.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="./css/a.css" rel="stylesheet" type="text/css">
<title>{$page_title}</title>
</head>
<body>
<p class="style1">新闻管理登陆 </p>
<hr>
<table width="771" height="501" border="0">
<tr>
<td height="62"><div align="center">系统管理</div></td>
<td width="666" rowspan="2"><form name="form1" method="post" action="index.php">
<p>标题
<input name="title" type="text" id="title" value="{$title}">
</p>
<p>内容:</p>
<p>{$editor}</p>
<p>
<input type="submit" name="Submit" value="提交">
<input type="hidden" name='action' value={$actionvalue}>
<input name="id" type="hidden" value="{$id}">
</p>
</form>
</td>
</tr>
<tr>
<td width="95" height="433"><div align="center">添加新闻</div></td>
</tr>
</table>
</body>
</html>
注:数据库已经在附件里面,先新建一个名为new的数据库,再把表导入
本系统用户名:admin 密码:admin
程序打包.rar (1.01 MB , 下载:0次)
PHP截取字符串 包函HTML标志的也可截取
作者:DH 日期:2009-06-25 13:48
最近需要在HTML的内容中提取一段文本作为简介,如果用普通的办法提取那么将有可能出现截取了半个HTML标志的情况,会破坏页面布局, 为此添加了这么一个函数,可完美解决此问题!!注意在这里你截取的字符数是不含HTML标志的!!
<?php
/**
* 截取HTML字符串 允许忽略HTML标志不计
*
* Author:学无止境
* Email:
* QQ: 339534039
* Blog:http://hi.baidu.com/phps
*
* 转载请保留作者信息
*
* @param 要截取的HTML $str
* @param 截取的数量 $num
* @param 是否需要加上更多 $more
* @return 截取串
*/
function phpos_chsubstr_ahtml($str,$num,$more=false)
{
$leng=strlen($str);
if($num>=$leng) return $str;
$word=0;
$i=0; /** 字符串指针 **/
$stag=array(array()); /** 存放开始HTML的标志 **/
$etag=array(array()); /** 存放结束HTML的标志 **/
$sp = 0;
&nb
PHP中二维数组的排序
作者:DH 日期:2009-05-22 10:51
- <?php
- // 说明:PHP中二维数组的排序方法
- /**
- * @package BugFree
- * @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $
- *
- *
- * Sort an two-dimension array by some level two items use array_multisort() function.
- *
- * sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2"……)
- * @author Chunsheng Wang <wwccss@263.net>
- * @param array $ArrayData the array to sort.
- * @param string $KeyName1 the first item to sort by.
- * @param string $SortOrder1 the order to sort by("SORT_ASC"|"SORT_DESC")
- * @param string $SortType1 the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")
- * @return array sorted array.
- */
- function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
- {
- if(!is_array($ArrayData))
- {
- return$ArrayData;
- }
- // Get args number.
- $Arg
缩略图程序 for ImageMagick for php(MagickWand)
作者:DH 日期:2008-12-03 16:16
留着吧,没测试,自己写了一个先将就用吧
/********************************************************************************
* MagicWand缩略图类(作为magicwand学习使用)
* 功能:按比例缩图,直接缩图,直接缩图时可选择截图范围,写文字/图片水印
* 已知问题:图片水印不能设置透明度,目当找不到相关函数和方法解决
* 作者:cwsky
* http://blog.me94me.com
* 2007-10-28 17:16:10
********************************************************************************/
class ImageZoom
{
var $fontArray = array(); //字体列表
var $markText = ""; //文字水印,文字
var $markImage = ""; //图片水印,文件名
var $markType = 0; //0:无水印,1:文字水印,2:图片水印
var $markAlpha = 0.5; //水印透明度
var $markTextColor = "#000000";
PHP中交替取出MYSQL中两个或多个表中的值 引
作者:DH 日期:2008-11-25 13:50
引用下吧
问题是这样的,有两个表S1、S2,二表结构和字段的属性完全相同。
现在需要在PHP当中取得这两个表中的值,当然可以先select * from S1取得所有所有的值赋给二维数组A[][]后再依样画葫芦select * from S2取得值赋给B[][];
但是,由于二表的结构完全相同,小可就想,能不能在取出S1赋值给A[][]的同时也取出S2赋值给B[][]?这样一来可以避免重复写编码,二来对于多个相同结构的表的处理时就比较方便了。
首先select s1.*, s2.* from s1,s2
是不可以的,这样可到的结果记录数是两表记录数的笛卡儿乘积,显然不符合题义
既然两表字段完全相同,读取数据时可用一个字定义函数。其实不相同也一样的
function get_recn($result_ori) {
for ( $temp_x = 0 ; $temp_x < $rows_ori ; $temp_x++)
{
$ori_row = mysql_fetch_array($result_ori);
$fieldname_ori = mysql_field_name($result_ori,0);
&nb
php mail class
作者:DH 日期:2008-07-11 11:46
<?php
/***********************************
PHPSo MVC System * Mail 邮件发送类
Lastdate:2005-12-16 9:30
QQ:10433182(Monkey)
Copyright:PHPSo.Com
**********************************
$mail = new email (
'smtp', //smtp方式方式时填写'smtp',mail函数发送时填写'mail'
//以下为smtp方式时需要填写的参数
'smtp.vip.bbn.cn', //邮箱smtp地址
'user', //邮箱帐户
'pass', //邮箱口令
'25', //端口,不填缺省为25
);
$mail->setCharset("gb2312");
$mail->setTo("收件人1 <mail1@163.com>,收件人2 <mail2@sina.com>"); //收件人
//$mail->setCC("mail3@21cn.com"); //抄送
//$mail-> setBCC("mail4@21cn.com,mail5@21cn.com"); //秘密抄送
$mail->setFrom("Monkey <monkeye@vip.bbn.cn>");//发件人
$mail->setSubject("email 标题") ; //主题
$mail->setText("文本格式") ;//发送文本格式也可以是变量
//$mail->setHTML("<title>主题</title>HTML格式") ;//发送html格式也可以是变量
$mail->setAttachments("c:/www/phpsomvc/_sample/5.htm|附件1,c:/www/phpsomvc/re
PHP里面实现JS的escape和unescape函数
作者:DH 日期:2008-06-13 15:44
function escape($str) {
preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(ord($v[0]) < 128)
$ar[$k] = rawurlencode($v);
else
$ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));
}
return join("",$ar);
}
function unescape($str) {
$str = rawurldecode($str);
preg_match_all("/(?:%u.{4})|.+/",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(substr($v,0,2) == "%u" && strlen($v) == 6)
$ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
}
return join("",$ar);
}
楼上的是没测试的
楼下的是测试的
function js_unescape($str)
{
$ret = '';
&n
