php eval具体用法

Filed under: 程序 | No Comments »
Posted on
  1. < ?php
  2.     $cache = "this is val";
  3.     function test($val){
  4.         global $cache;
  5.         $val = '$'.$val;
  6.         eval("\$str = $val;");
  7.         return $str;
  8.     }
  9.     echo test('cache');
  10. ?>

结果为:this is val
详情查看 »

原来mysql 编码转换 有更简单的办法

Filed under: 数据库 | No Comments »
Posted on

这是我之前用的转编码方法:把拉丁字符集的mysql数据表 转换成 utf8编码的

之前一直用这个方法转换mysql 的编码 从Latin1 到 UTF8 ,这次有一个70多w条的数据库,要把数据的编码从latin1转换到utf8,没想到使用这个方法,随着记录的增多,数据的插入越来越慢。

当把新表的主键删掉后,速度有了明显的变化,但新的数据条数反而比原来的还要多,真实BT,找不到原因,只好放弃采用删除主键的办法。(主键和索引对mysql的插入、修改、删除速度有影响。)

 问了一个朋友后,发现mysqldump 的时候加上–compatible=mysql40 参数,就可以指定编码了

mysqldump -uroot -proot – -compatible=mysql40 – -default-character-set=utf8 d5s_data > d5s.sql

详情查看 »

把mysql中的乱码变成正常的编码

Filed under: 数据库 | No Comments »
Posted on

使用phpmyadmin打开后,是乱码,但是查询出来,页面显示是正常的。(mysql使用utf8编码,数据库也是utf8编码。)

前台页面使用的是默认编码 gb2312,链接数据库后,也没有使用 set names

既然页面正常,那就好办,先把数据查出来,然后改写成sql语句,把sql当作文件来保存。

sql语句被保存后,这个时候还不能用,先把sql文件打开,用editplus另存为 utf8 格式。

用phpmyadmin 新建一个相同结构的数据库(不包括数据)

再使用phpmyadmin,载入这个sql文件,直接运行。或者是把sql语句复制到phpmyadmin中运行也行(但是文件太大的时候就太慢了。)

  1. <?php
  2.     $link = mysql_connect('localhost', 'root', 'root');
  3.     mysql_select_db('temp');
  4.  
  5.     $table = "article"; //指定表名
  6.     $query = 'SELECT * FROM '.$table;
  7.     $result = mysql_query($query);
  8.  
  9.     while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { 
  10.         $key = array();
  11.         $value = array();
  12.         foreach ($line as $k=>$v) { 
  13.             $key[] = $k;
  14.             $value[] = "'".str_replace("'", "&#39;", $v)."'";
  15.         } 
  16.         $sql[] = "INSERT INTO `".$table."`(".join(",", $key).") VALUES(".join(",", $value).");\r\n";
  17.     } 
  18.     file_put_contents("sql/".$table."_sql.php", $sql);
  19.  
  20.     mysql_free_result($result);
  21.     mysql_close($link);
  22. ?>

php 正则 不包含某字符串的正则表达式

Filed under: 程序 | No Comments »
Posted on

判断一个字符串中是否含有另一字符串,php有很多方法

常见函数 strstr($str, “abc”);

正则匹配 preg_match(”/(abc)?/is”, $str);

但是要匹配一个字符串中,不包含某字符串,用正则就比较麻烦了

如果不用正则 !strstr($str, “abc”); 就可以解决问题了

但是用正则呢,就只有这样了,”/^((?!abc).)*$/is”

  1. < ?php
  2. $str = "dfadfadf765577abc55fd";
  3. $pattern_url = "/^((?!abc).)*$/is";
  4. if (preg_match($pattern_url, $str)){
  5.     echo "不含有abc!";
  6. }else{
  7.     echo "含有abc!";
  8. }
  9. ?>

输出结果:含有abc!
详情查看 »

根据秒数,得到多少天多少小时多少分钟多少秒之前的事件

Filed under: 程序 | No Comments »
Posted on

计算两个时间差$time=$time1-$time2;

再算出时间差折合多少天多少秒,比如一天是24*60*60=86400秒,一小时是60*60=3600秒,如此如此

然后计算出相差多少天,向下取整,$days=floor($timedif/86400);

$days的值就是相差的天数,余数被略去了,当然可以用类似的操作再计算出余下多少小时多少分钟多少秒.

  1. <?php 
  2.  
  3.     $time = 5363;
  4.  
  5.     function interval_time($time){ 
  6.         $days = interval_day($time);        //多少天
  7.         $hour = interval_hour($time-86400*$days);
  8.         $minute = interval_minute($time-86400*$days-3600*$hour);
  9.         $second = interval_second($time-86400*$days-3600*$hour-60*$minute);
  10.        
  11.         $str = "";
  12.         $str.= $days ? $days."":"";
  13.         $str.= $hour ? $hour."小时":"";
  14.         $str.= $minute ? $minute."":"";
  15.         $str.= $second ? $second."":"";
  16.         return $str;
  17.     } 
  18.     function interval_day($time){ 
  19.         if ($time>=86400){ 
  20.             return floor($time/86400);        //多少天
  21.         } 
  22.     } 
  23.     function interval_hour($time){ 
  24.         if ($time>=3600 and $time<86400){ 
  25.             return floor($time/3600);        //多少小时
  26.         } 
  27.     } 
  28.     function interval_minute($time){ 
  29.         if ($time>=60 and $time<3600){ 
  30.             return floor($time/60);        //多少分钟
  31.         } 
  32.     } 
  33.     function interval_second($time){ 
  34.         if ($time>0 and $time<60){ 
  35.             return $time;        //多少秒
  36.         } 
  37.     } 
  38.  
  39.     echo interval_time($time);
  40. ?>

输出结果为:1小时29分23秒

php 比较多维数组大小 排序

Filed under: 程序 | No Comments »
Posted on

根据数组的键进行比较大小,排序,就不用说了,php有很多,很方便的内置函数,krsort(), ksort()等
如果是根据数组的值进行比较大小就麻烦一些。
如果值没有重复的情况,可以先用 array_flip() 交换键、值;然后krsort(),然后在array_flip()交换回来,就可以比较大小。
详情查看 »

正则查找出所有的图片src地址

Filed under: 程序 | 1 Comment »
Posted on

提取一段文本里的所有图片地址,所有的标准,不标准html代码格式,基本都考虑到了,如果还有其他情况,请留言。

  1. < ?php
  2.     $str = "<img src="http://www.d5s.cn/wp-admin/a.Gif" alt="\" /> <img src="http://www.d5s.cn/uploadfile/images/200511722531871.jpg" /> <img src="http://www.d5s.cn/b.jpG" /> <img src="http://www.d5s.cn/" /> <img src="http://www.d5s.cn/wp-admin/d.jpG" /> <img src="http://www.d5s.cn/" /><img src="http://www.d5s.cn/wp-admin/f.JPG" />";
  3. preg_match_all("/<img (.+?)src=('|\")?([^\s]+?)('|\"|\/>)/is", $str, $arr);
  4.     echo '<pre>';
  5.     print_r($arr[3]);
  6.     echo '</pre>';
  7. ?>

结果为:
Array
(
[0] => a.Gif
[1] => /uploadfile/images/200511722531871.jpg
[2] => /b.jpG
[3] => c.jpG
[4] => d.jpG
[5] => e.jpg
[6] => f.JPG
)
详情查看 »

FreeBSD 安裝 wget

Filed under: 服务器&PC | No Comments »
Posted on

在 Redhat/Fedora 是预设安装好 wget,而 FreeBSD 则预设没有安装,要在文本模式下载文件很不便。在 FreeBSD 要安装 wget 很容易,只要使用 ports 就行了,方法如下:

cd /usr/ports/ftp/wget
make install clean

那么系统就会自动替你完成安装了。

或者使用命令

pkg_add -r wget就可以把wget安装上去了

详情查看 »

不错的php图形类库 – JPGraph

Filed under: 程序 | No Comments »
Posted on

 今天看到一篇关于php图形类库的介绍,就转载过来了,说不定哪天会用上,就先记录一下。

以下转自: http://www.achome.cn/blog/?p=321

最近在用一个非常不错的php图形类库-JPGraph,来给各种统计数据绘图,感觉非常好用和强大,可以画各种统计图和曲线,也可以自定义展示颜色和字体等展示元素,和大家分享一下吧,下载地址如下:
http://download.chinaunix.net/download/0006000/5098.shtml

顺便附一个用jpgraph画柱状图的例子:

  1. include ("./graph/jpgraph.php");
  2. include ("./graph/jpgraph_bar.php");
  3.  
  4. $graph = new Graph(600,250,"auto");
  5. $graph->img->SetMargin(50,20,30,65);
  6. $graph->SetScale("textlin");
  7. $graph->SetShadow();
  8. $graph->title->Set("");
  9.  
  10. // Slightly adjust the legend from it's default position
  11. $graph->legend->SetLayout(LEGEND_HOR);
  12. $graph->legend->Pos(0.5,0.95,"center","bottom");
  13. $graph->legend->SetFont(FF_FONT1,FS_BOLD);
  14.  
  15. // Setup X-scale
  16. $graph->xaxis->SetTickLabels($xscal);//x坐标赋值
  17. $graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,8);//x坐标显示字体
  18. $graph->xaxis->SetLabelAngle(0);//x坐标显示角度
  19.  
  20. $bar = new BarPlot($speedArr);//赋值给柱状图
  21. $bar->SetFillColor("orange");//柱状图填充颜色
  22. $bar->value->Show();
  23.  
  24. $graph->Add($bar);
  25. // Output line
  26. $graph->Stroke();
分页: Prev 1 2 3 4 5 6 7 8 9 10 Next
1,995 垃圾评论
截获自
Akismet