1:如果打开的页面有乱码,别急,先不要做任何编辑。切记。
2:然后点击菜单修改-》页面属性-》标题/编码
3:在编码里选择 简体中文(gb2321)
4:点击 重新载入-》确定
新建一个记事本(不是写字板哦!),然后将那个要读取的文件全部复制到这个记事本文件中,保存。再用如下代码读取那个记事本文件1303275.txt:发现当网页编码为gb2312时全部正常显示。改为utf8编码后,数字正常,汉字为乱码,这也属于正常
<?php教程
$file = fopen("1303275.txt","r");//只读方式打开文本文件
while(! feof($file))//当文件不结束
{
$line=fgets($file);//读一行到$line变量
echo $line."<br />";
}
fclose($file);//关闭文本文件?>
上面的方法比较笨但也解决问题,下面我提供一个不管什么txt文本都不会乱的解决方法.
/*
@params $str 输入字符 $type 所需获取编码
@author 长行
*/
function autoiconv($str,$type = "gb2312//ignore"){$utf32_big_endian_bom = chr(0x00) . chr(0x00) . chr(0xfe) . chr(0xff);
$utf32_little_endian_bom = chr(0xff) . chr(0xfe) . chr(0x00) . chr(0x00);
$utf16_big_endian_bom = chr(0xfe) . chr(0xff);
$utf16_little_endian_bom = chr(0xff) . chr(0xfe);
$utf8_bom = chr(0xef) . chr(0xbb) . chr(0xbf);$first2 = substr($str, 0, 2);
$first3 = substr($str, 0, 3);
$first4 = substr($str, 0, 3);if ($first3 == $utf8_bom) $icon = 'utf-8';
elseif ($first4 == $utf32_big_endian_bom) $icon = 'utf-32be';
elseif ($first4 == $utf32_little_endian_bom) $icon = 'utf-32le';
elseif ($first2 == $utf16_big_endian_bom) $icon = 'utf-16be';
elseif ($first2 == $utf16_little_endian_bom) $icon = 'utf-16le';
else { $icon = 'ascii'; return $str;}return iconv($icon,$type,$str);
}
php教程 正则表达式实例详解(适合初学者)
数学公式正则表达式:
(?'kh'()*([-+]){0,1}[0-9.]+(?'-kh'))*([+-*/]{1}(?'kh'()*((?<=()([-+]){0,1})?[0-9.]+(?'-kh'))*)+(?('kh')(?!))
匹配加减乘除数学公式,如:((1+2)-3*4)
(?'kh'()*([-+]){0,1}[0-9.]+(?'-kh'))*([+-*/]{1}(?'kh'()*((?<=()([-+]){0,1})?[0-9.]+(?'-kh'))*)+(?('kh')(?!))
可以"("开始,压入堆栈统计括号数量,*代表可以出现0次或多次
(?'kh'()*([-+]){0,1}[0-9.]+(?'-kh'))*([+-*/]{1}(?'kh'()*((?<=()([-+]){0,1})?[0-9.]+(?'-kh'))*)+(?('kh')(?!))
可以出现一次正负号
(?'kh'()*([-+]){0,1}[0-9.]+(?'-kh'))*([+-*/]{1}(?'kh'()*((?<=()([-+]){0,1})?[0-9.]+(?'-kh'))*)+(?('kh')(?!))
0-9数字,一个或多个
(?'kh'()*([-+]){0,1}[0-9.]+(?'-kh'))*([+-*/]{1}(?'kh'()*((?<=()([-+]){0,1})?[0-9.]+(?'-kh'))*)+(?('kh')(?!))
可以")"结束,弹出堆栈统计括号数量,*代表可以出现0次或多次
(?'kh'()*([-+]){0,1}[0-9.]+(?'-kh'))*([+-*/]{1}(?'kh'()*((?<=()([-+]){0,1})?[0-9.]+(?'-kh'))*)+(?('kh')(?!))
橙色括号分组说明可以出现一次或多次,如公式1+2 ,这个括号里是匹配 2
红色要求+-*/出现一次
(?'kh'()*([-+]){0,1}[0-9.]+(?'-kh'))*([+-*/]{1}(?'kh'()*((?<=()([-+]){0,1})?[0-9.]+(?'-kh'))*)+(?('kh')(?!))
判断括号开始时,可以有正负号,?代表使用懒惰匹配,即如:1+(((2-3)+4)+5)时,只匹配数字2前面的一个括号一次
(?'kh'()*([-+]){0,1}[0-9.]+(?'-kh'))*([+-*/]{1}(?'kh'()*((?<=()([-+]){0,1})?[0-9.]+(?'-kh'))*)+(?('kh')(?!))
检查括号是否配对
原理简单,学过数据结构的一看就明白是什么道理了。不过今天在使用时数据中出现了子节点id(71)小于父节点id(104).导致部分子节点没被存储入数组
修改了一下
<?php教程
class tree
{
var $data = array();
var $child = array(-1=>array());
var $layer = array(-1=>-1);
var $parent = array();
var $num = array();
function setnode($id, $parent, $value,$num=0)
{
$parent = $parent ? $parent : 0;
$this->data[$id] = $value;
$this->num[$id] = $num;
if (!isset($this->child[$id])) $this->child[$id] = array();
$this->child[$parent][] = $id;
$this->parent[$id] = $parent;
if (!isset($this->layer[$parent]) && $parent == 0)
{
$this->layer[$id] = 0;
}
else
{
$this->layer[$id] = $this->layer[$parent] + 1;
}
}
function getlist(&$tree, $root= 0)
{
foreach ($this->child[$root] as $key=>$id)
{
$tree[] = $id;
if($this->child[$id]) $this->getlist($tree, $id);
}
}
function getvalue($id)
{
if($this->layer[$id]==0)
{
return $this->data[$id];
}
else
{
return $leftmar.$this->data[$id];
}
}
function getnum($id)
{
return $this->num[$id];
}
function getbitvalue($id)
{
return $this->data[$id];
}
function getlayer($id, $space = false)
{
return $space ? str_repeat($space, $this->layer[$id]) : $this->layer[$id];
}
function getparent($id)
{
return $this->parent[$id];
}
function getparents($id)
{
while ($this->parent[$id] != -1)
{
$id = $parent[$this->layer[$id]] = $this->parent[$id];
}
ksort($parent);
reset($parent);
return $parent;
}
function getchild($id)
{
return $this->child[$id];
}
function getchilds($id = 0)
{
$child = array($id);
$this->getlist($child, $id);
return $child;
}
function printdata()
{
return $this->layer;
}
}php树型菜单类
?>
首先来认识一下php教程提供的随机数函数rand()。php的rand()函数将返回随机整数,具体使用方法如下
rand(min,max)
可选参数min和max可以使rand() 返回0到rand_max之间的伪随机整数。例如,想要5到15(包括 5 和 15)之间的随机数,用 rand(5, 15)。
下面我来看一个具体的示例,我们做一个基本的函数调用,不设置具体的参数,我们得到的随机数将不受min和max两个参数的限制。
复制代码 代码如下:
<? echo(rand());?>
得到的结果:652696728 (随机结果)1、使用php生成指定区间内的随机数如果我们要生成两个数字之间的随机数,我们需要为rand设定两个参数:这样,我们得到的结果就在我们的控制之中,他应该是minnum <= result <= maxnum;假设我们要使用php生成10000到2000之间的随机数,我们的代码应该这样写:
复制代码 代码如下:
echo(rand(1000,2000));?>
够简单吧,下面来点稍微有些难度的。在本文的开始我们说过,随机数的作用很大,我们可以用php随机数解决一些复杂的问题。2、使用php获得一个集合中的随机元素将设我们需要从一个数组中获得一个随机元素
复制代码 代码如下:
$my_array=array('asp教程','php','网页特效','ajax','css教程','jquery','html');
echo($my_array[rand(0,6)]);
?>
可以想象,我们得到的结果可能是asp、php或javascript等任何一个包含在数组中的元素。注意,我们的my_array数组包含七个元素,我们将rand()的参数设置为0到6之间。下面我们使用两组随机数对上面的示例做功能增强,我们需要一个随机数进行条件判断,另一个随机数做元素的输出。
复制代码 代码如下:
$my_array=array('asp','php','javascript','ajax','css','jquery','html');
$repetition=rand(0,6);
for($i=0;$i<=$repetition;$i++){
echo('i am learning ' . $my_array[rand(0,6)]);
echo(' on 51cto.com');
}
?>
我们得到的结果可能像下面这样:
第一次运行我们得到三条结果
由于我们使用一个随机数限定显示的条数,所以得到的结果除了文章随机,显示的条数也是随机的,如下图:
第二次运行得到七条结果
也许你会问,php随机数只能做这些无聊的事儿吗?rand( )看来并不是那么重要;你错了,想想随处可见的验证码、一些cms系统的随机文章提取、下载地址分配等,随机数在这些应用中发挥着重要作用;另外,在安全和算法领域,随机数的很多应用也值得我们去深入研究,比如加密和同余法结构
<?php
$ip2id= round(rand(600000, 2550000) / 10000); //第一种方法,直接生成
$ip3id= round(rand(600000, 2550000) / 10000);
$ip4id= round(rand(600000, 2550000) / 10000);
//下面是第二种方法,在以下数据中随机抽取
$arr_1 = array("218","218","66","66","218","218","60","60","202","204","66","66","66","59","61","60","222","221","66","59","60","60","66","218","218","62","63","64","66","66","122","211");
$randarr= mt_rand(0,count($arr_1)-1);
$ip1id = $arr_1[$randarr];
echo $ip1id;
echo ".";
echo $ip2id;
echo ".";
echo $ip3id;
echo ".";
echo $ip4id;
?>
php教程创建文件时存在文件自动重命名实现方法
<?php
function createfile($filename, $content = '')
{
$fp = file_put_contents($filename, $content);
}
// 要创建的文件名称
$filename = 'www.txt';
if(file_exists($filename))
{
// 打开当前目录
$handle = opendir('./');
$fileinfo = pathinfo($filename);
//print_r($fileinfo);
$files = array();
while (false !== ($file = readdir($handle)))
{
if(preg_match_all("/{$fileinfo['filename']}(d*).{$fileinfo['extension']}/i", $file, $match))
{
$max = max($match[1]);
if($max)
{
$createfile = $fileinfo['filename'] . ($max + 1) . '.' . $fileinfo['extension'];
}
else
{
$createfile = $fileinfo['filename'] . '1.' . $fileinfo['extension'];
}
}
}
createfile($createfile);
}
else
{
createfile($filename);
}
?>
使用临时文件作记数器 方法二
<?php
function createfile($filename, $content = '')
{
if(file_exists($filename . '.tmp'))
{
$num = (int) file_get_contents($filename . '.tmp') + 1;
$fileinfo = pathinfo($filename);
file_put_contents($fileinfo['filename'] . '(' . $num . ')' .$fileinfo['extension'], $content);
file_put_contents($filename . '.tmp', $num);
}
else
{
file_put_contents($filename, $content);
file_put_contents($filename . '.tmp', 1);
}
}
createfile('test.txt');
?>