<meta http-equiv="content-type" content="text/html; charset=gb2312">
<?php
function tree($directory)
{
$mydir=dir($directory);
echo "<ul> ";
while($file=$mydir->read()){
if((is_dir("$directory/$file")) and ($file!=".") and ($file!=".."))
{echo "<li><font color="#ff00cc"><b>$file</b></font></li> ";
tree("$directory/$file");
}
else{
$a = explode('.',$file);
if($a[1] == 'html' || $a[1] == 'htm')
{
//unlink($file);
echo "<li>$file</li> ";
}
}
}
echo "</ul> ";
$mydir->close();
}
//开始运行
echo "<h2>目录为粉红色</h2><br> ";
tree("./");
?>
header("content-type: text/html;charset=gbk");//输出编码,避免中文乱码
?>
<html>
<head>
<title>ajax分页演示</title>
<script language="网页特效" >
var http_request=false;
function send_request(url){//初始化,指定处理函数,发送请求的函数
http_request=false;
//开始初始化xmlhttprequest对象
if(window.xmlhttprequest){//mozilla浏览器
http_request=new xmlhttprequest();
if(http_request.overridemimetype){//设置mime类别
http_request.overridemimetype("text/xml");
}
}
else if(window.activexobject){//ie浏览器
try{
http_request=new activexobject("msxml2.xmlhttp");
}catch(e){
try{
http_request=new activexobject("microsoft.xmlhttp");
}catch(e){}
}
}
if(!http_request){//异常,创建对象实例失败
window.alert("创建xmlhttp对象失败!");
return false;
}
http_request.onreadystatechange=processrequest;
//确定发送请求方式,url,及是否同步执行下段代码
http_request.open("get",url,true);
http_request.send(null);
}
//处理返回信息的函数
function processrequest(){
if(http_request.readystate==4){//判断对象状态
if(http_request.status==200){//信息已成功返回,开始处理信息
document.getelementbyid(reobj).innerhtml=http_request.responsetext;
}
else{//页面不正常
alert("您所请求的页面不正常!");
}
}
}
function dopage(obj,url){
document.getelementbyid(obj).innerhtml="<font color='green' font-size='12'>正在读取数据...</font>";
send_request(url);
reobj=obj;
}
</script>
<style>
/* css教程 document */
#result ul li{
height:20px;
width:auto;
display:block;
color:#999;
border:1px solid #999;
float:left;
list-style:none;
font-size:12px;
margin-left:5px;
line-height:20px;
vertical-align:middle;
text-align:center;
}
#result ul li a:link{
width:50px;
height:20px;
display:block;
line-height:20px;
background:#09c;
border:1px solid #fff;
color:#fff;
text-decoration:none;
}
#result ul li a:hover{
width:50px;
height:20px;
display:block;
line-height:20px;
background:#09c;
border:1px solid #fff;
color:#f60;
text-decoration:none;
}
</style>
</head>
<body>
<div id="result">
<?php
$page=isset($_get['page'])?intval($_get['page']):1; //这句就是获取page=18中的page的值,假如不存在page,那么页数就是1。
$num=10; //每页显示10条数据
$db=mysql教程_connect("localhost","root",""); //创建数据库教程连接
mysql_select_db("test"); //选择要操作的数据库
/*
首先咱们要获取数据库中到底有多少数据,才能判断具体要分多少页,具体的公式就是
总数据库除以每页显示的条数,有余进一。
也就是说10/3=3.3333=4 有余数就要进一。
*/
$result=mysql_query("select * from users");
$total=mysql_num_rows($result); //查询所有的数据
$url='test1.php';//获取本页url
//页码计算
$pagenum=ceil($total/$num); //获得总页数,也是最后一页
$page=min($pagenum,$page);//获得首页
$prepg=$page-1;//上一页
$nextpg=($page==$pagenum ? 0 : $page+1);//下一页
$offset=($page-1)*$num; //获取limit的第一个参数的值,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。
$pagenav="<ul>";
//开始分页导航条代码:
$pagenav.="<li>显示第 <b>".($total?($offset+1):0)."</b>-<b>".min($offset+10,$total)."</b> 条记录</li><li>共 $total 条记录 </li>";
//如果只有一页则跳出函数:
if($pagenum<=1) return false;
$pagenav.="<li> <a href=javascript:dopage('result','$url?page=1');>首页</a></li> ";
if($prepg) $pagenav.="<li> <a href=javascript:dopage('result','$url?page=$prepg');>前页</a></li> "; else $pagenav.=" <li>前页</li> ";
if($nextpg) $pagenav.="<li><a href=javascript:dopage('result','$url?page=$nextpg');>后页</a> </li>"; else $pagenav.=" <li>后页</li> ";
$pagenav.="<li> <a href=javascript:dopage('result','$url?page=$pagenum');>尾页</a></li> ";
$pagenav.="<li>第 $page 页</li><li>共 $pagenum 页</li></ul>";
//假如传入的页数参数大于总页数,则显示错误信息
if($page>$pagenum){
echo "error : can not found the page ".$page;
exit;
}
$info=mysql_query("select * from users limit $offset,$num"); //获取相应页数所需要显示的数据
while($it=mysql_fetch_array($info)){
echo $it['u_name'];
echo "<br>";
} //显示数据
echo"<br>";
echo $pagenav;//输出分页导航
?>
</div>
</body>
</html>
1、使用mbstring扩展库的mb_substr截取就不会出现乱码了。
2、自己书写截取函数,但效率不如用mbstring扩展库来得高。
3、如果仅是为了输出截取的串,可用如下方式实现:substr($str, 0, 30).chr(0)。
* 可以避免乱码的截取汉字
* 参数 $str 为字符串,$start 为开始字符,$len 结束字符
* 返回截取后的字符
*/
function msubstr($str, $start, $len) {
$tmps教程tr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen; $i++) {
if(ord(substr($str, $i, 1)) > 0xa0) {
$tmpstr .= substr($str, $i, 2);
$i++;
} else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}
//方法二 php实现中文字串截取无乱码的方法 www.111cn.net
function gbsubstr($string, $start, $length) {
if(strlen($string)>$length){
$str=null;
$len=$start+$length;
for($i=$start;$i<$len;$i++){
if(ord(substr($string,$i,1))>0xa0){
$str.=substr($string,$i,2);
$i++;
}else{
$str.=substr($string,$i,1);
}
}
return $str.'...';
}else{
return $string;
}
}
?>
$str ='13875884450';
if( preg_match("/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,8}$/",$str) || preg_match("/(?:13d{1}|15[03689])d{8}$/",$str))
{
echo '验证通过!';
}
else
{
echo '输入正确电话号码或手机号码!';
}
这是一款利用php程序来获取文件的大小并且把文件根据文件的大小显示gb,mb,kb代码的代码非常智能的代码。代码如下 | 复制代码 |
function sizecount($filesize) { //应用方法 |