首页 > php函数

目录遍历函数

PHP中的目录遍历功能本来也很普通,但它却具有一种"面向对象"的形式, 所以也提一下:
(1)dir, opendir
用法:
$d = dir("目录名");
$handle = opendir("目录名");
前者返回一个目录对象,后者返回一个目录句柄. 前者返回的对象有handle和path两个属性,第一个就相当于opendir 返回的句柄,第二个就是目录名本身.访问时用$d->handle和$d->path.
 
(2)read, readdir, rewind, rewinddir, close, closedir
三组中每组的前一个是目录对象的方法,用"对象->方法()"调用, 后一个是函数,用"函数名(目录句柄)"调用. read是返回目录中的下一个文件名. rewind是回到目录的第一个文件名. close是关闭目录,不再遍历.
 
(3)chdir
转换PHP的工作目录.

阅读全文

怎么样可以把 phpinfo()屏蔽掉?

Q:怎么样可以把 phpinfo()屏蔽掉?
A:路径:C:winntphp.ini(NT和2000)C:windows(95,98)
在 php.ini 配置文件里面有这个选项
disable_functions = ; This directive allows you to disable certain
; functions for security reasons. It receives
; a comma separated list of function names.
; This directive is *NOT* affected by whether
; Safe Mode is turned on or off.
改成
disble_functions = phpinfo

阅读全文

PHP中的DOMXML函数

DOM XML functions
These functions are only available if PHP was configured with --with-dom=[DIR], using the GNOME xml library. You will need at least libxml-2.0.0 (the beta version will not work). These functions have been added in PHP4.

This module defines the following constants:

Table 1. XML constants

Constant Value Description
XML_ELEMENT_NODE 1
XML_ATTRIBUTE_NODE 2
XML_TEXT_NODE 3
XML_CDATA_SECTION_NODE 4
XML_ENTITY_REF_NODE 5
XML_ENTITY_NODE 6
XML_PI_NODE 7
XML_COMMENT_NODE 8
XML_DOCUMENT_NODE 9
XML_DOCUMENT_TYPE_NODE 10
XML_DOCUMENT_FRAG_NODE 11
XML_NOTATION_NODE 12
XML_GLOBAL_NAMESPACE 1
XML_LOCAL_NAMESPACE 2

This module defines a number of classes. The DOM XML functions return a parsed tree of the XML document with each node being an object belonging to one of these classes.

xmldoc
(PHP4 >= 4.0b4)

xmldoc -- Creates a DOM object of an XML document
Description

object xmldoc (string str)


The function parses the XML document in str and returns an object of class "Dom document", having the properties "doc" (resource), "version" (string) and "type" (long).

xmldocfile
(PHP4 >= 4.0b4)

xmldocfile -- Creates a DOM object from XML file
Description

object xmldocfile (string filename)


The function parses the XML document in the file named filename and returns an object of class "Dom document", having the properties "doc" (resource), "version" (string).

xmltree
(PHP4 >= 4.0b4)

xmltree -- Creates a tree of php objects from XML document
Description

object xmltree (string str)


The function parses the XML document in str and returns a tree PHP objects as the parsed document.

阅读全文

字符串截取函数开始

字符串截取函数开始        function csubStr($str,$start,$len)
        {
                $strlen=strlen($str);
                $clen=0;
                for($i=0;$i<$strlen;$i++,$clen++)
                {
                        if ($clen>=$start+$len)
                        break;

                        if(ord(substr($str,$i,1))>0xa0)
                        {
                                if ($clen>=$start)
                                $tmpstr.=substr($str,$i,2);
                                $i++;
                        }
                        else
                        {
                                if ($clen>=$start)
                                $tmpstr.=substr($str,$i,1);
                        }
        }
       
        return $tmpstr;
        }
       
    //------------------------------字符串截取函数结束-------------------------------------------

/*使用方法:
<?php
$str=csubStr("ipod光环太耀眼 苹果的电脑本业该怎么办?",0,20);
echo $str;
?>       

阅读全文

magic_quotes_gpc使用方法


 对于magic_quotes_gpc有两种情况,第一种就是
magin_quotes_gpc=on
与magin_quotes_gpc=off
 下面我们就来举列说明.:
当magin_quotes_gpc=on时.

我们可以不对输入和输出数据库的字符串数据作 ,addslashes()和stripslashes()的操作,数据也会正常显示。

如果此时你对输入的数据作了addslashes()处理,那么在输出的时候就必须使用stripslashes()去掉多余的反斜杠。

2. 对于magic_quotes_gpc=off 的情况

必须使用addslashes()对输入数据进行处理,但并不需要使用stripslashes()格式化输出 因为addslashes()并未将反斜杠一起写入数据库,只是帮助mysql完成了sql语句的执行。

补充:

magic_quotes_gpc 作用范围是:WEB客户服务端;作用时间:请求开始时,例如当脚本运行时.
magic_quotes_runtime 作用范围:从文件中读取的数据或执行exec()的结果或是从SQL查询中得到的;作用时间:每次当脚本访问运行状态中产生的数据

阅读全文

PHP的unset()函数的实际效果


强烈建议大家没事就分享这种技术文章

PHP变量对内存的开销与释放,unset()是否真的释放内存。转自:PHP的unset()函数的实际效果
测试代码如下:
复制PHP内容到剪贴板
PHP代码:
for ( $i = 1; $i < 100; $i++ ) {
        $str = str_repeat('01234567', $i);
        $a = memory_get_usage();
        unset($str);
        $b = memory_get_usage();
        echo "n<br />".$i.': '.($b - $a).' Bytes.';
}


从结果看出:8 x 32 = 256 在256字节长的时候才真正有必要释放内存,有些人说,不如直接$str = null来的速度快。
下面是实际结果:
结果如下:
1: 0 Bytes.
2: 0 Bytes.
3: 0 Bytes.
4: 0 Bytes.
5: 0 Bytes.
6: 0 Bytes.
7: 0 Bytes.
8: 0 Bytes.
9: 0 Bytes.
10: 0 Bytes.
11: 0 Bytes.
12: 0 Bytes.
13: 0 Bytes.
14: 0 Bytes.
15: 0 Bytes.
16: 0 Bytes.
17: 0 Bytes.
18: 0 Bytes.
19: 0 Bytes.
20: 0 Bytes.
21: 0 Bytes.
22: 0 Bytes.
23: 0 Bytes.
24: 0 Bytes.
25: 0 Bytes.
26: 0 Bytes.
27: 0 Bytes.
28: 0 Bytes.
29: 0 Bytes.
30: 0 Bytes.
31: 0 Bytes.
32: -272 Bytes.
33: -280 Bytes.
34: -288 Bytes.
35: -296 Bytes.
36: -304 Bytes.
37: -312 Bytes.
38: -320 Bytes.
39: -328 Bytes.
40: -336 Bytes.
41: -344 Bytes.
42: -352 Bytes.
43: -360 Bytes.
44: -368 Bytes.
45: -376 Bytes.
46: -384 Bytes.
47: -392 Bytes.
48: -400 Bytes.
49: -408 Bytes.
50: -416 Bytes.
51: -424 Bytes.
52: -432 Bytes.
53: -440 Bytes.
54: -448 Bytes.
55: -456 Bytes.
56: -464 Bytes.
57: -472 Bytes.
58: -480 Bytes.
59: -488 Bytes.
60: -496 Bytes.
61: -504 Bytes.
62: -512 Bytes.
63: -520 Bytes.
64: -528 Bytes.
65: -536 Bytes.
66: -544 Bytes.
67: -552 Bytes.
68: -560 Bytes.
69: -568 Bytes.
70: -576 Bytes.
71: -584 Bytes.
72: -592 Bytes.
73: -600 Bytes.
74: -608 Bytes.
75: -616 Bytes.
76: -624 Bytes.
77: -632 Bytes.
78: -640 Bytes.
79: -648 Bytes.
80: -656 Bytes.
81: -664 Bytes.
82: -672 Bytes.
83: -680 Bytes.
84: -688 Bytes.
85: -696 Bytes.
86: -704 Bytes.
87: -712 Bytes.
88: -720 Bytes.
89: -728 Bytes.
90: -736 Bytes.
91: -744 Bytes.
92: -752 Bytes.
93: -760 Bytes.
94: -768 Bytes.
95: -776 Bytes.
96: -784 Bytes.
97: -792 Bytes.
98: -800 Bytes.
99: -808 Bytes.

阅读全文

简体中文转换为繁体中文的PHP函数

感谢网友Keyes提供移植用的Delphi源代码。其调用方式为$txt=gbtobig5($txt)。
(注:源代码中的include "data_gb.php";这个文件在就是一个数组,在http://caocao.oso.com.cn/data_gb.zip,请编辑下载到oso上,做一个链接,因为这个文件我过几天就要删除了。)
<?
/***********************************************************************
Written by caocao
caocao@eastday.com
http://caocao.oso.com.cn
With the help of Keyes
Keyes2000@263.net
http://my-wjl.scu.edu.cn/~Keyes
***********************************************************************/
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)
{
include "data_gb.php";
$output="";
$length=strlen($code);
$code=strtok($code,"");
$idx=0;
while ($idx < $length)
{
$tmpStr=$code[$idx].$code[$idx+1];
if (isgb($tmpStr))
{
$offset=gboffset($tmpStr);
if (($offset >= 0)||($offset <= 8177))
{
$output.=wordtostring($gborder[$offset]);
$idx++;
}
else
{
$output.= $code[$idx];
}
}
else
{
$output.= $code[$idx];
}
$idx++;
}
return ($output);
};
?>

阅读全文

一个阿拉伯数字转中文数字的函数

最近因需要,写了个“阿拉伯数字转中文数字的函数”。搜索了精华区只见到一个类似的。
感觉到我的算法不错,所以贴出来共享一下
如果要用于金额的转换,对小数部分的处理要做一下修改
<?php
function ch_num($num,$mode=true) {
$char = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
$dw = array("","拾","佰","仟","","孺","

阅读全文

PHP中的日期及时间

PHP有很多便于使用的函数以显示及处理日期。


要以某种特定格式显示日期或时间,可使用date()函数。它有两个参数:如何显示日期的格式以及代表你所要显示日期的时间戳。这个时间戳必须是先前所提到的从 1970 年起算的总秒数(如果你要使用当前时间可使用time()函数,此函数会返回“现在”的时间戳)。date() 有很多格式选项,如同C语言中的strftime()函数或Perl语言的POSIX::strftime()函数一样。
<?php
$birthday_stamp = mktime(19,45,0,3,10,1975);
$birthday_formatted = date('F d, Y - g:ia',$birthday_stamp);
echo "David was born on $birthday_formatted."
?>
会显示
David was born on March 10, 1975--7:45 p.m.
当然,如果你需要某已知的特定日期,这种复杂的格式函数并不会十分有用。因为你事先已经知道你的格式将会是什么。当在处理需要用户选择某日期的表单输出部分时,这些函数会比较有用:
<SELECT NAME="when">
<?php
$d = time();
for ($i = 0; $i < 10; $i++) {
echo '<OPTION VALUE="'.$d.'">'.date('F d',$d);
$d += 86400;
}
?>
</SELECT>
以上会输出一个单选框,其中有十个选项——今天及以后九天。在程序循环开始之前,我们将当前时间存放于变量$d中。每一个<OPTION>值会被显示,而其中的值会是以Unix时间戳计算,且所显示出来的文字设定为月、日(“July 27”、“July 28”等等)。在显示值后,变量$d 会被加上 86,400(是一天二十四小时的总秒数——24小时*60分钟*60秒)。
通过结合mktime()及date()函数,你就可以得出关于某特定用户输入日期的相关信息。那如果要寻找从某特定日期算起的第一个星期天(或者一周中的任意一天)呢?首先,先编写一个会输出适当格式的函数:
<?php
functiondisplay_form() {
global $PHP_SELF;
$dotw = array('Sunday','Monday','Tuesday','Wednesday','Thursday',
'Friday','Saturday');
$months = array( 1 => 'January','February','March','April','May','June',
'July','August','September','October','November','December');
?>
<FORM TARGET="<?php echo $PHP_SELF; ?>" METHOD=GET>

阅读全文

时间函数

PHP中的时间函数有这么些:
(1)date
用法:
date(格式,[时间]);
如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:
U 替换成从一个起始时间(好象是1970年1月1日)以来的秒数Y 替换成4位的年号.
y 替换成2位的年号.
F 替换成月份的英文全称.
M 替换成月份的英文简称.
m 替换成月份数.
z 替换成从当年1月1日以来的天数.
d 替换成日数.
l 替换成星期几的英文全称.
D 替换成星期几的英文简称.
w 替换成星期几(数字).
H 替换成小时数(24小时制).
h 替换成小时数(12小时制).
i 替换成分钟数.
s 替换成秒数.
A 替换成"AM"或"PM".
a 替换成"am"或"pm".
S 替换成序数字后缀,例如:"st","nd","rd","th".
函数返回作过了替换的格式串.
 
(2)getdate(时间)
返回一个哈希表,各下标是:
"seconds" -- 秒数
"minutes" -- 分数
"hours" -- 小时数
"mday" -- 日数
"mon" -- 月份数
"year" -- 年号
"yday" -- 1月1日以来的天数
"weekday" -- 星期几,英文全称
"month" -- 月份,英文全名
 
(3)gmdate
与date类似,但先将时间转换成格林威治标准时.
 
(4)mktime
用法:
mktime(小时数,分数,秒数,月,日,年); 返回一个时间值,可用于其他函数.
 
(5)time
用法:
time(); 返回1970年1月1日零点以来的秒数.
 
(6)microtime
用法:
microtime(); 返回一个字符串,用空格分成两部分,后一部分相当于time()
的返回值,前一部分是微秒数.

(7)checkdate
用法:
checkdate(月,日,年); 返回逻辑真或逻辑假. 如果:
年在1900和32767之间(包括1900与32767);
月在1到12之间;
日在该月的允许日数范围内(考虑了闰年);
则返回逻辑真.
 
(8)set_time_limit
用法:
set_time_limit(秒数);
规定从该句运行时起程序必须在指定秒数内运行结束, 超时则程序出错退出.

阅读全文

PHP中的日期处理

我正打算用PHP编写一种帮助处理系统。我发现我必须知道处理完最后一位客户的问题后已经过去了多长时间?当我过去用ASP时解决这个问题相当简单,ASP有相应的函数DateDiff可以给出两个日期间间隔多少月、多少天和多少秒。当我搜寻完PHP手册后我发现PHP并没有类似的函数。
本文包含以下内容:
1、 得到目前的日期和时间-我们有多少种方式?
2、 改变日期显示的方式-日期和时间的显示形式
3、 转换现在的日期为Unix的时间戳值
4、 改变日期
a. 增加时间
b. 减去时间
c. 找出两日期之间的间隔
5、 为PHP添加DateAdd函数
6、 为PHP添加DateDiff函数
**得到目前的日期和时间
在Unix中,时间的表示方式为计算从1970年1月1日零时起所过去的秒数,这称为UNIX 时间戳(Unix Epoch)。
如果我们有这样一段的代码:
?
echo time();
?
将返回值958905820
而此时的时间为2000年5月21日12时43分。
你也许会说这相当不错。当这对我毫无帮助,或者只有一点帮助。在PHP中,对日期处理的函数都必须用到由time()返回的时间戳值。同时,由于PHP在Unix和Windows系统中均使用同样的时间戳值,这就允许你不需要修改代码即可在不同的系统间移植。另外的一个好处是time()函数返回的是一个整数,你可以将其作为整数字段或文本字段存入数据库,而不必使用特别的日期/时间字段。
你已经基本了解了Unix的时间戳值,现在让我们来展示它的实际用途。
改变日期显示的方式-日期和时间的显示形式
PHP提供两个办法来将Unix的时间戳值转换成为有用的数据。第一个是date()函数。这个函数有两个参数-第一个字符串用于设定你所希望返回的格式,第二个为Unix的时间戳值。
格式化字符串通过一些简单的特殊格式化字符来显示你所希望看到的格式的日期和时间。假设你希望日期以这样的格式显示“18h01 Sunday 21 May”。
我们需要对字符串中的每一部分使用一个特殊格式化字符,你可以从PHP手册中日期和时间函数库中找到。这样的特殊格式化字符数量不少,他们所表示的类似于星期几、月的英文名、用2位或4位数表示的年份,是否是上午(AM)或下午(PM)以及其他。对于这个例子我们需要的特殊字符为:
'H’ -24 小时制的小时
'i’- 分钟
'l’- 星期几的英文全名
'd’- 本月的第几日
'F’- 月份的英文全名
因此我们的格式化字符串为”Hhi l d F”, PHP代码为:

阅读全文

PHP操作MySQL的函数

(1)mysql_connect(主机,用户名,口令);
返回一个连接号.
注意:mysql各用户的口令可以随该用户所在机器IP地址不同而改变.
另外,mSQL没有用户名机制,所以msql_connect只需要一个主机参数.
主机可以是IP地址或域名.
(2)mysql_create_db(数据库名);
(3)mysql_select_db(数据库名,连接号);
连接一个数据库.
(4)mysql_query(SQL语句,连接号);
如果SQL语句是select,则返回一个结果号.否则返回的值可以不理会.
如果失败,返回false.
(5)mysql_fetch_array(结果号);
取出下一行,返回一个数组.可以用数字下标访问(第一个字段是下标
0),也可以用字符串下标访问(即使用各字段名).
如已取了最后一行,返回false.
(6)mysql_fetch_field(结果号,[字段序号]);
如无字段序号,取下一个字段.
返回一个哈希表,下标有:
name,table,max_length,not_null,primary_key,unique_key,
multiple_key,numeric,blob,type,unsigned,zerofill
各下标的意思应该比较明白了.
(7)mysql_num_rows(结果号);mysql_num_fields(结果号);
(8)mysql_free_result(结果号);
(9)mysql_list_dbs();mysql_list_tables(数据库名);
(10)mysql_close(连接号);
(11)mysql_pconnect(主机,用户名,口令);
(12)mysql_select_db(数据库名,连接号);
连接一个数据库.
与mysql_connect完全相似,但建立一个"永久连接",该连接一经建立永不
关闭,即使使用mysql_close函数或程序执行完毕也不关闭.下一次试图建
立永久连接时,系统如发现已存在一个永久连接,则直接返回该连接号而
不重新创建.

阅读全文