首页 > php代码

BMForum

BMForum 拥有论坛程序中前所未有的领先技术和舒适的用户体验。
国内首家 打破传统分类方式 —— 主题随意贴(Tags、标签)
只要发帖时指定一个 Tag,就可以令帖子联系更紧密!
先进的 AJAX 技术 普遍用于发帖和浏览
快速翻页浏览、快速回复,一切如飞。
 
国内首家 论坛邀请注册方式
论坛用户可以通过自己的努力邀请其他人加入!
根据 XHTML1.0 + CSS 标准构建程序
论坛程序完全符合W3C国际标准,风格制作更简单。
 
国内首家 可视化发帖,让发帖更轻松
通过可视化发帖,方便、安全地张贴想要的内容
强大的后台管理,方便的快速定位
通过论坛程序内置的“我想”菜单方便找到你所想的
高效的论坛性能
论坛程序负载承受能力空前绝后
全球首家 多附件同时上传、附件图文混排
使用 BMForum 论坛程序,论坛发帖易如反掌
国内首家 领先的多国语言 UTF-8 编码
国际、港澳台交流更紧密
强大的用户组权限控制系统
所有用户根据论坛程序中的用户组权限识别权限
下载
http://www.bmforum.com/down/

阅读全文

轻型数据库SQLite结合PHP的开发

SQLite是一款轻型的数据库,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如Tcl、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源世界闻名的数据库治理系统来讲,它的处理速度比他们都快。

SQLite虽然很小巧,但是支持的SQL语句不会逊色于其他开源数据库,它支持的SQL包括:

ATTACH DATABASE
BEGIN TRANSACTION
comment
COMMIT TRANSACTION
COPY
CREATE INDEX
CREATE TABLE
CREATE TRIGGER
CREATE VIEW
DELETE
DETACH DATABASE
DROP INDEX
DROP TABLE
DROP TRIGGER
DROP VIEW
END TRANSACTION
EXPLAIN
expression
INSERT
ON CONFLICT clause
PRAGMA
REPLACE
ROLLBACK TRANSACTION
SELECT
UPDATE

同时它还支持事务处理功能等等。也有人说它象Microsoft的Access,有时候真的觉得有点象,但是事实上它们区别很大。比如SQLite支持跨平台,操作简单,能够使用很多语言直接创建数据库,而不象Access一样需要Office的支持。假如你是个很小型的应用,或者你想做嵌入式开发,没有合适的数据库系统,那么现在你可以考虑使用SQLite。目前它的最新版本是 3.2.2,它的官方网站是:http://www.sqlite.org,能在上面获得源代码和文档。同时因为数据库结构简单,系统源代码也不是很多,也适合想研究数据库系统开发的专业人士。

现在我们开始简单的介绍,主要我是想讲清楚几个问题,一是如何安装使用,二是如何跟PHP结合开发。

一、安装

1. Windows平台

下载windows下的文件,其实就是一个命令行程序,(下载地址:http://www.sqlite.org/sqlite-3_2_2.zip),这个命令行程序用来包括生成数据库文件、执行SQL查询、备份数据库等等功能。
下载后比如我们解压缩到 D:Downloadssqlitesqlite-3_2_2 这个目录下,那么我们进入cmd,并且进入该目录:
cd D:Downloadssqlitesqlite-3_2_2
D:Downloadssqlitesqlite-3_2_2>sqlite3 test.db
# 假如test.db不存在,那么就产生一个数据库文件,假如存在就直接使用该数据库文件,相当于mysql中的use
SQLite version 3.2.2
Enter ".help" for instructions
sqlite>
# SQLite的提示符,假如想查看命令帮助输入 .help,在sqlite中所有系统命令都是 . 开头的:
sqlite> .help
.databases List names and files of attached databases
.dump ?TABLE? ... Dump the database in an SQL text format
.echo ON|OFF Turn command echo on or off
.exit Exit this program
.explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
.header(s) ON|OFF Turn display of headers on or off
.help Show this message
.import FILE TABLE Import data from FILE into TABLE
.indices TABLE Show names of all indices on TABLE
.mode MODE ?TABLE? Set output mode where MODE is one of:
csv Comma-separated values
column Left-aligned columns. (See .width)
html HTML <table> code
insert SQL insert statements for TABLE
line One value per line
list Values delimited by .separator string
tabs Tab-separated values
tcl TCL list elements
.nullvalue STRING Print STRING in place of NULL values
.output FILENAME Send output to FILENAME
.output stdout Send output to the screen

阅读全文

管理权限分组说明

文涉及权限管理的一种面向对象模型的方法和实现。通过分析每次访问发生场景的各要素,并对各要素进行抽象而形成的一种模型,并可用于实现权限访问控制。原谅我自己取了什么“四维权限管理模型”“访问控制矩阵(ACM)”这样难听的名字,还多少有故弄玄虚之嫌,但我在半年前只有这样的见识。

阅读全文

实现跨域名Cookie

Cookie真是一个伟大的发明,它答应web开发者保留他们的用户的登录状态。然而,当你的站点或网络
有一个以上的域名时就会出现问题了。

  在Cookie规范上说,一个cookie只能用于一个域名,不能够发给其它的域名。因此,假如在浏览器中对一个域名设置了一个cookie,这个cookie对于其它的域名将无效。假如你想让你的用户从你的站点中的其中一个进行登录,同时也可以在其它域名上进行登录,这可真是一个大难题。

  我的解决方案将使用下面的一般框架:

一个预置的脚本将用来接受通过GET或COOKIE方式传递过来的sessionid号。它将比COOKIE优先选择GET
变量。所以,无论何时需要引用交叉的域名时,我们把sessionid做为一个URL参数进行发送。修改Apache配置,用来实现重写所有的交叉域名的cookie。这样做的原因一会儿就会清楚了。在任何时候出现一个交叉域名引用时使用变量。

第一步:创建预置脚本
  将下面的代码加到预置脚本中(或出现在所有脚本之前的函数中)。


<?php

/* 支持交叉域名cookie... */

// 假如GET变量已经设置了,并且它与cookie变量不同
//则使用get变量(更新cookie)
global $HTTP_COOKIE_VARS, $HTTP_GET_VARS;
if (isset($sessionid) && isset($HTTP_GET_VARS['sessionid']) && ($HTTP_COOKIE_VARS['sessionid'] != $HTTP_GET_VARS['sessionid'])) {
SetCookie('sessionid', $HTTP_GET_VARS['sessionid'], 0, '/', '');
$HTTP_COOKIE_VARS['sessionid'] = $HTTP_GET_VARS['sessionid'];
$sessionid = $HTTP_GET_VARS['sessionid'];
}
?>

  一旦这个代码运行之后,一个全局的'sessionid'变量将可以用于脚本。它将保存着用户的cookie中的
sessionid值,或者是通过GET请求发来的sessionid值。


第二步:为所有的交叉域名引用使用变量
  创建一个全局的配置文件,用于存放可以进行切换的域名的基本引用形式。例如,假如我们拥有
domain1.com和domain2.com,则如下设置:


<?php
$domains['domain1'] = "http://www.domain1.com/-$sessionid-";
$domains['domain2'] = "http://www.domain2.com/-$sessionid-";
?>

  现在,假如在代码中如下做:


<?php
echo "Click &lt;a href="", $domains['domain2'], "/contact/?email=yes"&gt;here&lt;/a&gt; to contact us.";
?>

阅读全文

又装了一次~eAccelerator [安装步聚]

wget http://bart.eaccelerator.net/source/0.9.5.2/eaccelerator-0.9.5.2.tar.bz2 tar jxf eaccelerator-0.9.5.2.tar.bz2 [@wcms_web eaccelerator-0.9.5.2]# export PHP_PREFIX='/usr/local/bin'
[@wcms_web eaccelerator-0.9.5.2]# $PHP_PREFIX/phpize
★ ./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/php-config (PHP4版本编译) ◆ ./configure --enable-eaccelerator=shared --with-eaccelerator-shared-memory --with-php-config=$PHP_PREFIX/php-config (php5版本需要加 --with-eaccelerator-shared-memory) #上面~需要拼到whereis phpize /usr/local/bin/phpize 这个目录即可 [@wcms_web eaccelerator-0.9.5.2]# make
[@wcms_web eaccelerator-0.9.5.2]# make install
Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20050922/
创建专用的缓存目录:
mkdir /tmp/eaccelerator
chmod 0777 /tmp/eaccelerator 然后设置 php.ini, 重启一下apache:
/home/apache/bin/apachectl restart 说明PHP 与 eAccelerator 也需要版本对称才可以. 现PHP Version 5.1.6 与~eaccelerator-0.9.5.1就可以了~ ########################################################################### 设置 php.ini 如下设置:;eAccelerator
extension="/usr/local/lib/php/extensions/no-debug-non-zts-20050922/eaccelerator.so"
eaccelerator.shm_size = "1024"
eaccelerator.cache_dir = "/var/cache/eAcache"
eaccelerator.enable = "1"
eaccelerator.optimizer = "1"
eaccelerator.debug = "0"
eaccelerator.check_mtime = "1"
eaccelerator.filter = ""
eaccelerator.shm_max = "0"
eaccelerator.shm_ttl = "7200"
eaccelerator.shm_only = "1"
eaccelerator.compress = "1"
eaccelerator.compress_level = "3"
eaccelerator.keys = "shm_only"
eaccelerator.sessions = "shm_only"
eaccelerator.content = "shm_only"
--------------------------------------------------------------------------------
;extension="eaccelerator.so"
zend_extension="/usr/lib/php4/eaccelerator.so"
;zend_extension_ts="/usr/lib/php4/eaccelerator.so"
;extension="eaccelerator.dll"
;zend_extension_ts="c:\php4\eaccelerator.dll"
;zend_extension="c:\php4\eaccelerator.dll" 您必须取消一条注释 (而且同时只能是一条被取消) 以便使用下面的描述来装载 eAccelerator 的扩展.
eAccelerator 兼容于 Zend Optimizer’s loader. Zend Optimizer 在 eAccelerator 的安装结束进行后必须在 php.ini 中配置. 假如您不 使用用脚本来编码的 Zend Encoder ,那么我们不推荐您安装成 Zend Optimizer 加上 eAccelerator 这种模式.
--------------------------------------------------------------------------------
eaccelerator.shm_size = "64" eAccelerator 可以使用的共享内存的数量 (以兆为单位) . "0" 是指操作系统的默认值. 默认值是 "0".
--------------------------------------------------------------------------------
eaccelerator.cache_dir = "/tmp/eaccelerator" 这个目录是给磁盘缓存使用. eAccelerator 在这里储存预先编译好的代码, 进程数据, 内 容以及用户的自定义内容. 同样的数据也能被储存在共享内存中 (这样可以提高访问速度). 默认的设置是 "/tmp/eaccelerator".
--------------------------------------------------------------------------------
eaccelerator.enable = "1" 打开或者关闭 eAccelerator. "1" 是指打开, "0" 是指关闭. 默认值是 "1".
--------------------------------------------------------------------------------
eaccelerator.optimizer = "1" 打开或者关闭内置的 peephole optimizer,它可以提高代码的执行速度. "1" 是指打开, "0" 是指关闭. 默认 值是 "1".
--------------------------------------------------------------------------------
eaccelerator.debug = "0" 打开或者关闭调试记录. "1" 是指打开, "0" 是指关闭. 默认值是 "0".
--------------------------------------------------------------------------------
eaccelerator.check_mtime = "1" 打开或者关闭 PHP 的文件修改检查. "1" 是指打开, "0" 是指关闭. 假如您在修改以后重新编译 PHP 的文 件,那么您应当设置为 "1". 默认值是 "1".
--------------------------------------------------------------------------------
eaccelerator.filter = "" 决定哪些 PHP 文件应该被缓存. 您可以指定一个范围 (比如 "*.php *.phtml"),这样被指定的文件就会被缓存或 者被排出在外. 假如使用字符 "!" 来指定这个范围, 符合这个范围的文件就被排出在外. 默认值是 "" ,意思是说 - 所有的 PHP 脚本都会被 缓存.
--------------------------------------------------------------------------------
eaccelerator.shm_max = "0" 利用 "eaccelerator_put()" 这个功能选项来关闭在共享内存中加载大量数据. 它指出最大的被答应的大小,以 字节为单位 (10240, 10K, 1M). 使用 "0" 来关闭这个限制. 默认值是 "0".

阅读全文

php常用正则表达精品

^d+$  //匹配非负整数(正整数 + 0)
^[0-9]*[1-9][0-9]*$  //匹配正整数
^((-d+)|(0+))$  //匹配非正整数(负整数 + 0)
^-[0-9]*[1-9][0-9]*$  //匹配负整数
^-?d+$    //匹配整数
^d+(.d+)?$  //匹配非负浮点数(正浮点数 + 0)
^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$  //匹配正浮点数
^((-d+(.d+)?)|(0+(.0+)?))$  //匹配非正浮点数(负浮点数 + 0)
^(-(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*)))$  //匹配负浮点数
^(-?d+)(.d+)?$  //匹配浮点数
^[A-Za-z]+$  //匹配由26个英文字母组成的字符串
^[A-Z]+$  //匹配由26个英文字母的大写组成的字符串
^[a-z]+$  //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$  //匹配由数字和26个英文字母组成的字符串
^w+$  //匹配由数字、26个英文字母或者下划线组成的字符串
^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$    //匹配email地址
^[a-zA-z]+://匹配(w+(-w+)*)(.(w+(-w+)*))*(?S*)?$  //匹配url


利用正则表达式去除字串中重复的字符的算法程序:

var s="abacabefgeeii"
var s1=s.replace(/(.).*1/g,"$1")
var re=new RegExp("["+s1+"]","g")
var s2=s.replace(re,"")
alert(s1+s2) //结果为:abcefgi
===============================
如果var s = "abacabefggeeii"
结果就不对了,结果为:abeicfgg
正则表达式的能力有限


1.确认有效电子邮件格式
下面的代码示例使用静态 Regex.IsMatch 方法验证一个字符串是否为有效电子邮件格式。如果字符串包含一个有效的电子邮件地址,则 IsValidEmail 方法返回 true,否则返回 false,但不采取其他任何操作。您可以使用 IsValidEmail,在应用程序将地址存储在数据库中或显示在 ASP.NET 页中之前,筛选出包含无效字符的电子邮件地址。

[Visual Basic]
Function IsValidEmail(strIn As String) As Boolean
' Return true if strIn is in valid e-mail format.
Return Regex.IsMatch(strIn, ("^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$")
End Function
[C#]
bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
}


2.清理输入字符串
下面的代码示例使用静态 Regex.Replace 方法从字符串中抽出无效字符。您可以使用这里定义的 CleanInput 方法,清除掉在接受用户输入的窗体的文本字段中输入的可能有害的字符。CleanInput 在清除掉除 @、-(连字符)和 .(句点)以外的所有非字母数字字符后返回一个字符串。

[Visual Basic]
Function CleanInput(strIn As String) As String
' Replace invalid characters with empty strings.
Return Regex.Replace(strIn, "[^w.@-]", "")
End Function
[C#]
String CleanInput(string strIn)
{
// Replace invalid characters with empty strings.
return Regex.Replace(strIn, @"[^w.@-]", "");
}


3.更改日期格式
以下代码示例使用 Regex.Replace 方法来用 dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。

[Visual Basic]
Function MDYToDMY(input As String) As String
Return Regex.Replace(input, _
"b(?<month>d{1,2})/(?<day>d{1,2})/(?<year>d{2,4})b", _
"${day}-${month}-${year}")
End Function
[C#]
String MDYToDMY(String input)
{
return Regex.Replace(input,
"\b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b",
"${day}-${month}-${year}");
}
Regex 替换模式
本示例说明如何在 Regex.Replace 的替换模式中使用命名的反向引用。其中,替换表达式 ${day} 插入由 (?<day>...) 组捕获的子字符串。

有几种静态函数使您可以在使用正则表达式操作时无需创建显式正则表达式对象,而 Regex.Replace 函数正是其中之一。如果您不想保留编译的正则表达式,这将给您带来方便


4.提取 URL 信息
以下代码示例使用 Match.Result 来从 URL 提取协议和端口号。例如,“http://www.contoso.com:8080/letters/readme.html”将返回“http:8080”。

[Visual Basic]
Function Extension(url As String) As String
Dim r As New Regex("^(?<proto>w+)://[^/]+?(?<port>:d+)?/", _
RegexOptions.Compiled)
Return r.Match(url).Result("${proto}${port}")
End Function
[C#]
String Extension(String url)
{
Regex r = new Regex(@"^(?<proto>w+)://[^/]+?(?<port>:d+)?/",
RegexOptions.Compiled);
return r.Match(url).Result("${proto}${port}");
}

阅读全文

PHP正则验证类


 

<?php
/**
 *PHP正则验证类
 *Code by T.T.R
 *[url]http://www.Gx3.cn[/url] [url]http://Gx3.cn[/url]
 *QQ:252319874
 */
class regExp
{


    static function strTrim($str)
    {
        return preg_replace("/s/","",$str);
    }
 


    static function userName($str,$type,$len)
    {
        $str=self::strTrim($str);
        if($len<strlen($str))
        {
            return false;
        }else{
            switch($type)
            {
                case "EN"://纯英文
                    if(preg_match("/^[a-zA-Z]+$/",$str))
                    {
                        return true;
                    }else{
                        return false;
                    }
                    break;
                case "ENNUM"://英文数字
                    if(preg_match("/^[a-zA-Z0-9]+$/",$str))
                    {
                        return true;
                    }else{
                        return false;
                    }
                    break;
                case "ALL":    //允许的符号(|-_字母数字)
                    if(preg_match("/^[|-_a-zA-Z0-9]+$/",$str))
                    {
                        return true;
                    }else{
                        return false;
                    }
                    break;
            }
        }
    }
 


    static function passWord($min,$max,$str)
    {
        $str=self::strTrim($str);
        if(strlen($str)>=$min && strlen($str)<=$max)
        {
            return true;
        }else{
            return false;
        }
    }


    static function Email($str)
    {
        $str=self::strTrim($str);
       
        if(preg_match("/^([a-z0-9_]|-|.)+@(([a-z0-9_]|-)+.){1,2}[a-z]{2,4}$/i",$str))
        {
            return true;
        }else{
            return false;
        }
       
    }


    static function idCard($str)
    {
        $str=self::strTrim($str);
        if(preg_match("/^([0-9]{15}|[0-9]{17}[0-9a-z])$/i",$str))
        {
            return true;
        }else{
            return false;
        }
    }
  

 

    static function Phone($type,$str)
    {
        $str=self::strTrim($str);
        switch($type)
        {
            case "CHN":
                if(preg_match("/^([0-9]{3}|0[0-9]{3})-[0-9]{7,8}$/",$str))
                {
                    return true;
                }else{
                    return false;
                }
                break;
            case "INT":
                if(preg_match("/^[0-9]{4}-([0-9]{3}|0[0-9]{3})-[0-9]{7,8}$/",$str))
                {
                    return true;
                }else{
                    return false;
                }
                break;
        }
    }
}
$str="008-010-2711204";
if(regExp::Phone("INT",$str))
{
    echo "ok";
}else{
    echo "no";
}
?>
 

阅读全文

网页文字简繁转换函数

<?
function isgb($code)
{
if (strlen($code)>=2)
{
$code=strtok($code,"");
if ((ord($code[0]) < 161)||(ord($code[0]) >= 247))
{
return (0);
}
else
{
if ((ord($code[1]) <= 161)||(ord($code[1]) >= 254))
{
return (0);
}
else
{
return (1);
}
}
}
else
{
return (1);
}
}

function gboffset($code)
{
if (strlen($code) >= 2)
{
$code=strtok($code,"");
return ((ord($code[0]) - 161) * 94 (ord($code[1]) - 161));
}
else
{
return(-1);
}
}

function wordtostring($code)
{
return (chr(hexdec(substr($code,0,2))).chr(hexdec(substr($code,2,2))));
}

function gbtobig5($code)

阅读全文

PHP生成静态页面注意几点


一 : 引 言
在速度上,静态页面要比动态页面的比方php快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权限限制等,但是,对应一些我们经常频频使用的文件,比方说,开发的新闻发布系统,我们不希望很多用户都读取数据库才显示结果,这样一方面消耗了服务器的资源,另一方面占去了浏览者大量可贵的响应时间,所有,有了"静态页面话"的做法,当前很多网站都采用这种技术,一般都是由管理后台控制,或者生成html直接显示,或者xhtml用css控制显示,或者生成xml用xslt显示,这些技术都不是难的,在这里我就浅显的说说生成html的方法.

二 : 预备知识
   
     模板技术:


[PHP]模板引擎Smarty深入浅出介绍--2005-12-31
[PHP]笑谈配置,使用Smarty技术 2006-01-04

     缓存技术:   

有些信息比方经常不变的,但是还是能变的信息放在缓存中以加快显示速度,这是很有价值的,所谓的缓存,通俗的理解就是一些保存在服务器端的共用信息.它是于服务器同生死的,我们在保存缓存的时候可以指定下次更新的时间的判断,比方要在5分钟更新一次,可以记录上次更新的时间,和当前时间比较,如果大于 5 分钟 ,读取数据库,更新换成,否则直接读取缓存数据,当然,缓存需要客户端用户激活的,只需一次.

ob_start()函数:打开输出缓冲区.
    函数格式 void ob_start(void)
    说明:当缓冲区激活时,所有来自PHP程序的非文件头信息均不会发送,而是保存在内部缓冲区。为了输出缓冲区的内容,可以使用ob_end_flush()或flush()输出缓冲区的内容。

Flush:刷新缓冲区的内容,输出。
    函数格式:flush()
    说明:这个函数经常使用,效率很高。

ob_get_contents :返回内部缓冲区的内容。
    函数格式:string ob_get_contents(void)
    说明:这个函数会返回当前缓冲区中的内容,如果输出缓冲区没有激活,则返回 FALSE.

ob_get_length:返回内部缓冲区的长度。
    函数格式:int ob_get_length(void)
    说明:这个函数会返回当前缓冲区中的长度;和ob_get_contents一样,如果输出缓冲区没有激活,则返回 FALSE.

ob_end_clean:删除内部缓冲区的内容,并且关闭内部缓冲区
    函数格式:void ob_end_clean(void)
    说明:这个函数不会输出内部缓冲区的内容而是把它删除

ob_end_flush:发送内部缓冲区的内容到浏览器,并且关闭输出缓冲区
    函数格式:void ob_end_flush(void)
    说明:这个函数发送输出缓冲区的内容(如果有的话)

ob_implicit_flush:打开或关闭绝对刷新
    函数格式:void ob_implicit_flush ([int flag])
    说明:默认为关闭缓冲区,打开绝对输出后,每个脚本输出都直接发送到浏览器,不再需要调用 flush()     

     文件写入:   
int fwrite ( resource handle, string string [, int length] )
fwrite() 把 string 的内容写入 文件指针 handle 处。 如果指定了 length,当写入了 length 个字节或者写完了 string 以后,写入就会停止,视乎先碰到哪种情况。
fwrite() 返回写入的字符数,出现错误时则返回 FALSE 。
相关参考官方网站: 文件参考

三 : 解决方案

思路: 开启 ob_start缓冲,当已经调出数据的时候获取 ob_get_contents,然后生成静态页,ob_end_clean清除缓冲.ok,就这么来,来看一个例子(php+mysql的结合):
创建数据库:

CREATE TABLE `bihtml` (
  `id` int(11) NOT NULL auto_increment,
  `szdtitle` varchar(16) NOT NULL,
  `szdcontent` text NOT NULL,
  PRIMARY KEY  (`id`)  
) TYPE=MyISAM;



获取当前的ID,并导入模板:

ob_start();
$id=_POST['id']
if(!isset($id)&&is_integer($id))
{
        @$db=new mysqli('localhost','root','admin','bihtml');
        $result=$db->fetch_one_array("select  * from szd_bi where id='$id'");
                        if(!empty($result))
                        {       
                        $tmp->assign(array(
                                "Szdtitle",htmlspecialchars($result['titles']),
                                "Szdcontent",$result['titles']));
                        }
        $tpl->display('default_1.tpl');
        $this_my_f= ob_get_contents();        //此处关键
        ob_end_clean();
        $filename = "$id.html";
        if(tohtmlfile_cjjer($filename,$this_my_f))
        echo "生成成功 $filename";
        else
        echo "生成识别";
        }
}

//把生成文件的过程写出函数
function tohtmlfile_cjjer($file_cjjer_name,$file_cjjer_content)
{
        if (is_file ($file_cjjer_name)){
                @unlink ($file_cjjer_name);
        }
$cjjer_handle = fopen ($file_cjjer_name,"w");
        if (!is_writable ($file_cjjer_name)){
                return false;
        }
        if (!fwrite ($cjjer_handle,$file_cjjer_content)){
                return false;
        }
fclose ($cjjer_handle); //关闭指针
return $file_cjjer_name;
}




四 : 说明事项

1: 一般建议管理员添加数据的时候就生成静态页面,可以考虑记录生成的文件名次和路径.

2:php主要是    ob_starts()和 ob_get_contents,生成静态页面的时候很有用,当然也可以考虑调出数据库直接替换模板里面的变量也是可以的.

3:主要的模板使用smarty,phplib都是可以的,smarty使用比较简易.

阅读全文

无GD PNG图片生成对象



<?php
/*
* PNG图片接口文件
*
* 这不是一个免费程序
*
* @开发时间:20080109
* @开发者:张旭 [email]cszx2007@gmail.com[/email]
* @版本:1.0
* @版权:张旭持有
*/
interface zxpngInterface
{
    /*生成图片(图片宽度, 图片高度, 图片内容)*/
    function build($width, $height, $content);
}
/*
* PNG图片生成程序
*
* 这不是一个免费程序
*
* @开发时间:2008年1月8日 18:14
* @开发者:zx
* @版本:1.0
* @版权:张旭持有
*/
class zxpng implements zxpngInterface
{
    private $kernel;
    /*
    * 库主控文件
    *
    * @var kernel
    * @access private
    */
    private $status;
    /*
    * 处理状态说明
    *
    * @var bool
    * @access private
    */
   
    private $info;
    /*
    * png图片信息集合
    *
    * @var array
    * @access private
    */
   
    public function __construct($kernel)
    {
        $this->kernel = &$kernel;
    }
   
    /*生成图片(图片宽度, 图片高度, 图片内容)*/
    public function build($width, $height, $content)
    {
        self::createBackground($width, $height);
        $contentLength = strlen($content);
        $table = self::table( );
        $space = 12;
       
        for($i = 0; $i < $contentLength; $i++)
        {
            self::draw($table[$content[$i]], $width, $height, $space);
            $space += 12;
        }
       
        self::crcTable( );
        $data = self::headTag( );
        $data .= self::ihdr($width, $height);
        $data .= self::idat($width, $height);
        $data .= self::iend( );
        return($data);
    }
   
    /*创建背景颜色(宽度, 高度)*/
    private function createBackground($width, $height)
    {
        $rnd1 = mt_rand(90, 0);
        $rnd2 = mt_rand(90, 0);
        $rnd3 = mt_rand(90, 0);
        $data = '';
       
        for($i = 0; $i < $height; $i++)
        {
            for($j = 0; $j < $width; $j++)
            {
                $data .= chr((($i * $j) + $width + $height + $rnd1) & 255) . chr($rnd2 & 255) . chr($rnd3 & 255);
            }
        }
        return(self::save('img', $data));
    }
   
    /*绘制像素点(x, y, 宽度, 高度, r, g, b, 像素大小)*/
    private function drawPixel($x, $y, $width, $height, $r=0, $g=0, $b=0, $size=1)
    {
        $img = self::read('img');
        $offset = 3 * ($width * $y + $x);
       
        for($i = 0; $i < $size;)
        {
            $img[$offset + $i] = chr($r);
            $i++;
            $img[$offset + $i] = chr($g);
            $i++;
            $img[$offset + $i] = chr($b);
            $i++;
        }
       
        return(self::save('img', $img));
    }
   
    /*绘制字符(字模, 宽度, 高度, 间隔)*/
    private function draw($table, $width, $height, $space=16)
    {
        for($i = 0; $i < 16; $i++)
        {
            for($j = 0; $j < 8; $j++)
            {
                if(($table[$i] >> (7 - $j)) & 0x01)
                {
                    self::drawPixel($space + $j, $i + 2, $width, $height, 255, 255, 255, 1);
                }
            }
        }
       
        return(true);
    }
   
    /*ascii码字模表*/
    private function table( )
    {
        $ascii = array
                (
                    '0' => array(0x00,0x00,0x38,0x6C,0xC6,0xC6,0xD6,0xD6,0xC6,0xC6,0x6C,0x38,0x00,0x00,0x00,0x00),
                    '1' => array(0x00,0x00,0x18,0x38,0x78,0x18,0x18,0x18,0x18,0x18,0x18,0x7E,0x00,0x00,0x00,0x00),
                    '2' => array(0x00,0x00,0x7C,0xC6,0x06,0x0C,0x18,0x30,0x60,0xC0,0xC6,0xFE,0x00,0x00,0x00,0x00),
                    '3' => array(0x00,0x00,0x7C,0xC6,0x06,0x06,0x3C,0x06,0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00),
                    '4' => array(0x00,0x00,0x0C,0x1C,0x3C,0x6C,0xCC,0xFE,0x0C,0x0C,0x0C,0x1E,0x00,0x00,0x00,0x00),
                    '5' => array(0x00,0x00,0xFE,0xC0,0xC0,0xC0,0xFC,0x06,0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00),
                    '6' => array(0x00,0x00,0x38,0x60,0xC0,0xC0,0xFC,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00),
                    '7' => array(0x00,0x00,0xFE,0xC6,0x06,0x06,0x0C,0x18,0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00),
                    '8' => array(0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7C,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00),
                    '9' => array(0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7E,0x06,0x06,0x06,0x0C,0x78,0x00,0x00,0x00,0x00),
                    'A' => array(0x00,0x00,0x10,0x38,0x6C,0xC6,0xC6,0xFE,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00),
                    'B' => array(0x00,0x00,0xFC,0x66,0x66,0x66,0x7C,0x66,0x66,0x66,0x66,0xFC,0x00,0x00,0x00,0x00),
                    'C' => array(0x00,0x00,0x3C,0x66,0xC2,0xC0,0xC0,0xC0,0xC0,0xC2,0x66,0x3C,0x00,0x00,0x00,0x00),
                    'D' => array(0x00,0x00,0xF8,0x6C,0x66,0x66,0x66,0x66,0x66,0x66,0x6C,0xF8,0x00,0x00,0x00,0x00),
                    'E' => array(0x00,0x00,0xFE,0x66,0x62,0x68,0x78,0x68,0x60,0x62,0x66,0xFE,0x00,0x00,0x00,0x00),
                    'F' => array(0x00,0x00,0xFE,0x66,0x62,0x68,0x78,0x68,0x60,0x60,0x60,0xF0,0x00,0x00,0x00,0x00),
                    'G' => array(0x00,0x00,0x3C,0x66,0xC2,0xC0,0xC0,0xDE,0xC6,0xC6,0x66,0x3A,0x00,0x00,0x00,0x00),
                    'H' => array(0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xFE,0xC6,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00),
                    'I' => array(0x00,0x00,0x3C,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00),
                    'J' => array(0x00,0x00,0x1E,0x0C,0x0C,0x0C,0x0C,0x0C,0xCC,0xCC,0xCC,0x78,0x00,0x00,0x00,0x00),
                    'K' => array(0x00,0x00,0xE6,0x66,0x66,0x6C,0x78,0x78,0x6C,0x66,0x66,0xE6,0x00,0x00,0x00,0x00),
                    'L' => array(0x00,0x00,0xF0,0x60,0x60,0x60,0x60,0x60,0x60,0x62,0x66,0xFE,0x00,0x00,0x00,0x00),
                    'M' => array(0x00,0x00,0xC6,0xEE,0xFE,0xFE,0xD6,0xC6,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00),
                    'N' => array(0x00,0x00,0xC6,0xE6,0xF6,0xFE,0xDE,0xCE,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00),
                    'O' => array(0x00,0x00,0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00),
                    'P' => array(0x00,0x00,0xFC,0x66,0x66,0x66,0x7C,0x60,0x60,0x60,0x60,0xF0,0x00,0x00,0x00,0x00),
                    'Q' => array(0x00,0x00,0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xD6,0xDE,0x7C,0x0C,0x0E,0x00,0x00),
                    'R' => array(0x00,0x00,0xFC,0x66,0x66,0x66,0x7C,0x6C,0x66,0x66,0x66,0xE6,0x00,0x00,0x00,0x00),
                    'S' => array(0x00,0x00,0x7C,0xC6,0xC6,0x60,0x38,0x0C,0x06,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00),
                    'T' => array(0x00,0x00,0x7E,0x7E,0x5A,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00),
                    'U' => array(0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00),
                    'V' => array(0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x6C,0x38,0x10,0x00,0x00,0x00,0x00),
                    'W' => array(0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xD6,0xD6,0xD6,0xFE,0xEE,0x6C,0x00,0x00,0x00,0x00),
                    'X' => array(0x00,0x00,0xC6,0xC6,0x6C,0x7C,0x38,0x38,0x7C,0x6C,0xC6,0xC6,0x00,0x00,0x00,0x00),
                    'Y' => array(0x00,0x00,0x66,0x66,0x66,0x66,0x3C,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00),
                    'Z' => array(0x00,0x00,0xFE,0xC6,0x86,0x0C,0x18,0x30,0x60,0xC2,0xC6,0xFE,0x00,0x00,0x00,0x00),
                    'a' => array(0x00,0x00,0x00,0x00,0x00,0x78,0x0C,0x7C,0xCC,0xCC,0xCC,0x76,0x00,0x00,0x00,0x00),
                    'b' => array(0x00,0x00,0xE0,0x60,0x60,0x78,0x6C,0x66,0x66,0x66,0x66,0x7C,0x00,0x00,0x00,0x00),
                    'c' => array(0x00,0x00,0x00,0x00,0x00,0x7C,0xC6,0xC0,0xC0,0xC0,0xC6,0x7C,0x00,0x00,0x00,0x00),
                    'd' => array(0x00,0x00,0x1C,0x0C,0x0C,0x3C,0x6C,0xCC,0xCC,0xCC,0xCC,0x76,0x00,0x00,0x00,0x00),
                    'e' => array(0x00,0x00,0x00,0x00,0x00,0x7C,0xC6,0xFE,0xC0,0xC0,0xC6,0x7C,0x00,0x00,0x00,0x00),
                    'f' => array(0x00,0x00,0x38,0x6C,0x64,0x60,0xF0,0x60,0x60,0x60,0x60,0xF0,0x00,0x00,0x00,0x00),
                    'g' => array(0x00,0x00,0x00,0x00,0x00,0x76,0xCC,0xCC,0xCC,0xCC,0xCC,0x7C,0x0C,0xCC,0x78,0x00),
                    'h' => array(0x00,0x00,0xE0,0x60,0x60,0x6C,0x76,0x66,0x66,0x66,0x66,0xE6,0x00,0x00,0x00,0x00),
                    'i' => array(0x00,0x00,0x18,0x18,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00),
                    'j' => array(0x00,0x00,0x06,0x06,0x00,0x0E,0x06,0x06,0x06,0x06,0x06,0x06,0x66,0x66,0x3C,0x00),
                    'k' => array(0x00,0x00,0xE0,0x60,0x60,0x66,0x6C,0x78,0x78,0x6C,0x66,0xE6,0x00,0x00,0x00,0x00),
                    'l' => array(0x00,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00),
                    'm' => array(0x00,0x00,0x00,0x00,0x00,0xEC,0xFE,0xD6,0xD6,0xD6,0xD6,0xC6,0x00,0x00,0x00,0x00),
                    'n' => array(0x00,0x00,0x00,0x00,0x00,0xDC,0x66,0x66,0x66,0x66,0x66,0x66,0x00,0x00,0x00,0x00),
                    'o' => array(0x00,0x00,0x00,0x00,0x00,0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00),
                    'p' => array(0x00,0x00,0x00,0x00,0x00,0xDC,0x66,0x66,0x66,0x66,0x66,0x7C,0x60,0x60,0xF0,0x00),
                    'q' => array(0x00,0x00,0x00,0x00,0x00,0x76,0xCC,0xCC,0xCC,0xCC,0xCC,0x7C,0x0C,0x0C,0x1E,0x00),
                    'r' => array(0x00,0x00,0x00,0x00,0x00,0xDC,0x76,0x66,0x60,0x60,0x60,0xF0,0x00,0x00,0x00,0x00),
                    's' => array(0x00,0x00,0x00,0x00,0x00,0x7C,0xC6,0x60,0x38,0x0C,0xC6,0x7C,0x00,0x00,0x00,0x00),
                    't' => array(0x00,0x00,0x10,0x30,0x30,0xFC,0x30,0x30,0x30,0x30,0x36,0x1C,0x00,0x00,0x00,0x00),
                    'u' => array(0x00,0x00,0x00,0x00,0x00,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0x76,0x00,0x00,0x00,0x00),
                    'v' => array(0x00,0x00,0x00,0x00,0x00,0x66,0x66,0x66,0x66,0x66,0x3C,0x18,0x00,0x00,0x00,0x00),
                    'w' => array(0x00,0x00,0x00,0x00,0x00,0xC6,0xC6,0xD6,0xD6,0xD6,0xFE,0x6C,0x00,0x00,0x00,0x00),
                    'x' => array(0x00,0x00,0x00,0x00,0x00,0xC6,0x6C,0x38,0x38,0x38,0x6C,0xC6,0x00,0x00,0x00,0x00),
                    'y' => array(0x00,0x00,0x00,0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x7E,0x06,0x0C,0xF8,0x00),
                    'z' => array(0x00,0x00,0x00,0x00,0x00,0xFE,0xCC,0x18,0x30,0x60,0xC6,0xFE,0x00,0x00,0x00,0x00)
                );
       
        return($ascii);
    }
   
    /*创建CRC校验表*/
    private function crcTable( )
    {
        $z = -306674912;  // = 0xedb88320
       
        for ($n = 0; $n < 256; $n++)
        {
            $c = $n;
           
            for($k = 0; $k < 8; $k++)
            {
                $c = ($c & 1) ? $z ^ (($c >> 1) & 0x7fffffff) : ($c >> 1) & 0x7fffffff;
            }
           
            $crcTable[$n] = $c;
        }
       
        return(self::save('crcTable', $crcTable));
    }
   
    /*png图片文件头*/
    private function headTag( )
    {
        return(pack("c*", 137, 80, 78, 71, 13, 10, 26, 10));
    }
   
    /*IHDR数据块信息(宽度, 高度)*/
    private function ihdr($width, $height)
    {
        $body = pack("c*", ($width >> 24) & 255, ($width >> 16) & 255, ($width >> 8) & 255, $width & 255, ($height >> 24) & 255, ($height >> 16) & 255, ($height >> 8) & 255, $height & 255, self::depth( ), self::type( ), 0, 0, 0);
        return(self::chunk($body, "IHDR"));
    }
   
    /*图像深度*/
    private function depth( )
    {
        return(8);
    }
   
    /*图像类型*/
    private function type( )
    {
        return(2);
    }
   
    /*数据块(数据, 类型)*/
    private function chunk($data, $type)
    {
        $len = strlen($data);
        $chunk = pack("c*", ($len >> 24) & 255, ($len >> 16) & 255, ($len >> 8) & 255, $len & 255) . $type . $data;
        $c = $z = 16777215 | 255 << 24;
        $chunkLength = strlen($chunk);
        $crcTable = self::read('crcTable');
       
        for ($i = 4; $i < $chunkLength; $i++)
        {
             $z8 = ($z >> 8) & 0xffffff;
             $z = $crcTable[($z ^ ord($chunk[$i])) & 0xff] ^ $z8;
        }
       
        $crc = $c ^ $z;
        $chunk .= chr(($crc >> 24) & 255) . chr(($crc >> 16) & 255) . chr(($crc >> 8) & 255) . chr($crc & 255);
        return($chunk);
    }
   
    /*IDAT数据块(宽度, 高度)*/
    private function idat($width, $height)
    {
        $data = self::read('img');
        $body = '';
        $k = 0;
       
        for($h = 0; $h < $height; $h++)
        {
            $body .= chr(0);
           
            for($w = 0; $w < $width * 3; $w++)
            {
                 $body .= $data[$k++];
            }
        }
       
        return(self::chunk(self::idatHead($width, $height) . $body . self::adler($body), "IDAT"));
    }
   
    /*IDAT头信息(宽度, 高度)*/
    private function idatHead($width, $height)
    {
        $len = ($width * 3 + 1) * $height;
        return(pack("c*", 0x78, 0x01, 1, $len & 255, ($len >> 8) & 255, 255 - ($len & 255), 255 - (($len >> 8) & 255)));
    }
    /*计算adler32码(内容数据)*/
    private function adler($data)
    {
        $dataLen = strlen($data);
        $s1 = 1;
        $s2 = 0;
       
        for($i = 0; $i < $dataLen; $i++)
        {
            $s1 = ($s1 + ord($data[$i])) % 65521;
            $s2 = ($s2 + $s1) % 65521;
        }
       
        $adler = ($s2 << 16) | $s1;
        return(chr(($adler >> 24) & 255) . chr(($adler >> 16) & 255) . chr(($adler >> 8) & 255) . chr($adler & 255));
    }
   
    /*iend*/
    private function iend( )
    {
        return(self::chunk('', "IEND"));
    }
   
    /*保存(字段名, 值)*/
    private function save($field, $value)
    {
        $this->info[$field] = $value;
        return(true);
    }
   
    /*读取(字段名)*/
    private function read($field)
    {
        return($this->info[$field]);
    }
   
    public function __destruct( )
    {
    }
}
?>
 

阅读全文

php 正则表达式

最近在写bbs中,遇上代码转换问题。寻找了很久,才得到一个比较完善的解决办法,可以彻底还原发文者的原文。
以下贴出,供大家指正。
系统:linux+php4+oracle8i

<?
//--标题,名字等字段入库处理(去首尾空格)
function trans_string_trim($str) {
  $str=trim($str);
  $str=eregi_replace(""","""",$str);
  $str=stripslashes($str);
  return $str;
}

//--文章入库处理,即textarea字段;
function trans_string($str) {
  $str=eregi_replace(""","""",$str);
  $str=stripslashes($str);
  return $str;
}

//--从库中显示在表单中;在text中以trans转换,在textarea中,无需转换,直接显示

//--显示在web页面,过滤html代码;包括链接地址
function trans($string) {
  $string=htmlspecialchars($string);
  $string=ereg_replace(chr(10),"<br>",$string);
  $string=ereg_replace(chr(32)," ",$string);
  return $string;   
}

//--显示在web页面,不过滤html代码;
function trans_web($string) {
  $string=ereg_replace(chr(10),"<br>",$string);
  $string=ereg_replace(chr(32)," ",$string);
  return $string;   
}

//--显示在web页面,过滤html代码及头尾空格,主要用于显示用户昵称
function trans_trim($string) {
  $string=trim($string);
  $string=htmlspecialchars($string);
  $string=ereg_replace(chr(10),"<br>",$string);
  $string=ereg_replace(chr(32)," ",$string);
  return $string;   
}

//--显示在span中;
function trans_span($string) {
  $string=ereg_replace(chr(10),"/n",$string);
  $string=ereg_replace(chr(32)," ",$string);
  $string=ereg_replace(""",""",$string);
  return $string;   
}

//--在web上显示cookie,过滤html
function trans_cookie($str) {
  $str=trans($str);
  $str=stripslashes($str);
  $str=eregi_replace("''","'",$str);
  return $str;
}
?>
---------------------------
最后,顺带补充一点,如果在span中显示文章中的一段,采用substr取定长字符串时,记得在span的参数后面多加一个空格,否则遇上截到半个汉字时,会搞乱html代码。

阅读全文