PHP

浏览模式: 标准 | 列表 | 图文
JpGraph中文乱码完美解决方案[转]

JpGraph中文乱码完美解决方案[转]

原文地址:http://lizi.blogbus.com/logs/12601515.html

很多人使用JpGraph会出现乱码问题,但是网上有好多并无效果的解决方式,经过反复试验及在网上看了其它人的经验,总结出在2.3版下最终较完美解决方案
1、把simsun.ttc字体拷到服务器/usr/X11R6/lib/X11/fonts/truetype下,当然这个目录可以自定义
2、不需要更改$aFF === FF_SIMSUN或$aFF === FF_CHINESE ,直接用以下代码即可
$title = "JpGraph中文测试";
$title = iconv("UTF-8", "gb2312", $title);
$graph->title->Set($title);
就可以完美的解决中文问题了
3、还有一个问题,就是右上方的中文显示问题,就是SetLegend函数,解决方式如下
打开jpgraph.php文件,找到
private $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;
用
private $font_family=FF_SIMSUN,$font_style=FS_NORMAL,$font_size=8;
替换就OK了^_^

后记:本来都忘了这个了,正好有朋友问起类似的问题,于是又GOOGLE了一下,找到了这个,记录一下,也为GOOGLE的收录作点贡献,这样以后再有人遇到类似问题,更容易被搜索到。

利用array_diff函数更新tag

利用array_diff函数更新tag

先介绍一下array_diff函数

array_diff -- 计算数组的差集
说明
array array_diff ( array array1, array array2 [, array ...] )


array_diff() 返回一个数组,该数组包括了所有在 array1 中但是不在任何其它参数数组中的值。注意键名保留不变。

注意: 两个单元仅在 (string) $elem1 === (string) $elem2 时被认为是相同的。也就是说,当字符串的表达是一样的时候。

注意: 注意本函数只检查了多维数组中的一维。当然可以用 array_diff($array1[0], $array2[0]); 检查更深的维度。

::::::本函数在 PHP 4.0.4 中是坏的! :::::

在网站的实际应用中,我们可能会用到TAG(标签),对标签的更新可能也会经常遇到,当然添加了之后不修改另作别论。

比如,我们为一条新闻添加了如下标签:新闻,CGX,艳照门 (开个玩笑,呵呵),存入数据库后,应该是在tag表里存入类似于这样的数据

id newsid tagname
1 1 新闻
2 1 CGX
3 1 艳照门

其中id是自增字段,newsid是新闻的ID,tagname是刚刚存入的标签(存的时候以逗号分隔)。

然而现实总是残酷的,XXX总局规定我们不得把“艳照门”作为关键字了,于是,我们不得不更新TAG(当然,直接从数据库里delete from table where tagname='艳照门'更方便,但这里我是在举例),于是新的TAG就可能变成了:新闻,CGX,其他图片。更新的时候,我们要从数据库里把“艳照门”去除掉,但怎么知道我把原来的标签“艳照门”删除了呢?这时候,array_diff就有用了。

<?php
//隐藏字段:原始标签
$_POST['origin_tags'] = '新闻,CGX,艳照门';
//标签字段
$_POST['news_tags'] = '新闻,CGX,其他图片';

$origin_tags = explode( ',', trim( $_POST['origin_tags'] ) );
$new_tags = explode( ',', trim( $_POST['news_tags'] ) );

$diff_tags = array_diff( $origin_tags , $new_tags );

echo "<pre>";
print_r ( $diff_tags ) ;
echo "</pre>";

?>

看,我们把“艳照门”打印出来了,于是,下一步我们就可以执行:DELETE FROM tablename WHERE news_id = 2 AND tagname = '艳照门';来删除这条记录,同时将新的数据插入数据库(新的数据判断重复可以采用array_intersect,使用方法与array_diff类似,这里就不提了,当然更方便的方法是把:$diff_tags = array_diff( $origin_tags , $new_tags ); 改为 $diff_tags = array_diff( $new_tags , $origin_tags );)

更新数据的方法有很多种,我在这里只是提出一个解决方案。希望大家能够给出其他更好的建议。

Easy way to find a file in subdirectory with php

Easy way to find a file in subdirectory with php

原文地址:http://blog.makemepulse.com/2008/02/18/tips-easy-way-to-find-a-file-in-subdirectory-with-php/

<?php
class DirectorySearch {

private
$iterator,
$directoryname;
public function __construct($directoryname){
$this->iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directoryname,RecursiveDirectoryIterator::KEY_AS_FILENAME));
}

public function find($filename){
foreach($this->iterator as $entry){
if($filename == $this->iterator->current()){
return $this->iterator->getPathName();
}
}
return false;
}
}

实际应用:

<?php
$directory = dirname(__FILE__)."/directory";

$search_directory = new DirectorySearch($directory);
$path_file = $search_directory->find("file_to_find.php");

if($path_file !== false){
//include_once($path_file);
echo $path_file;
}

If we don’t use the KEY_AS_FILENAME with the recursiveDirectoryIterator we will have the whole path of the file in $this->iterator->current();

如果我们不使用recursiveDirectoryIterator类的KEY_AS_FILENAME,我们也可以使用文件的全路径$this->iterator->current();

All of this is a basic example of the RecursiveIteratorIterator with the RecursiveDirectoryIterator. SPL is a very powerfull toolkit !

这只是RecursiveDirectoryIterator的一个基本的功能,不得不说,SPL是一个非常好的工具。

自己的话:

以前遍历目录找文件一般是两种方法:1、opendir,2、glob,然后来遍历整个目录。如今SPL里有现成的,那肯定是这样的速度更快,效率更高喽?只是。。。不知道对中文文件名的支持会怎么[已测试,让我失望]

<?php
function read_dir( $dir) {
$array = array();
$d = dir($dir);
while (false !== ($entry = $d->read())) {
if($entry!='.' && $entry!='..') {
$entry = $dir.'/'.$entry;
if(is_dir($entry)) {
$array[] = $entry;
$array = array_merge($array, read_dir($entry));
} else {
$array[] = $entry;
}
}
}
$d->close();
return $array;
}
function get_file ($file, $file_path ){
foreach ( $file_path as $_k => $_v ){
if ( basename($_v) == $file ){
return $_v ;
}
}

}
?>

测试下来。。。让我失望。

<?php
$directory = dirname(__FILE__)."/Discuz!_6.0.0_SC_UTF8";
$sTime = microtime(true);

$search_directory = new DirectorySearch($directory);
$path_file = $search_directory->find("buy中文.htm");

if($path_file !== false){
//include_once($path_file);
echo( $path_file );
}
echo '<pre>';
print_r( microtime(true) - $sTime );
echo '</pre>';

$sTime = microtime(true);
$f = 'buy中文.htm';
echo( get_file($f,read_dir($directory)) );
echo '<pre>';
print_r( microtime(true) - $sTime );
echo '</pre>';

使用CLass的时间是0.86550688743591

使用function的时间是:0.54081797599792

鉴定完毕:支持中文文件名

PHP连贯接口

PHP连贯接口

  近期,看到很多人都在讨论连贯接口这个东西,仔细想想,这其实没有什么,用的早的如javascript中的jQuery,都早就在用了。只是PHP最近被人翻出来炒一炒而已。

  但不得不说的是,连贯接口也有它自身的好处,即可以让代码更清晰化,比如Zend的Select类里面,生成一个SQL并执行就是那样的直观:

<?php
$db = new Zend_Db(); //这是随便写的,主要是为了显示一下
$db->select()
->from('tablename')
->where('id = ?' , $id)
->orwhere('name != ?' , $name);

//最后也可以调用一下来生成这个SQL
$sql = $db->__toString();

自从到了PHP5,$a = new Object() ; $b = $a; 这个$b 直接变成了取址,而不是COPY后,这样的连贯接口比之用数组来实现效率是高上了很多,毕竟相对占用内存会少一点吧?(估计,没有实测过)。

不过,这东西确实也就和AJAX一样,属于老瓶装新酒。以前类里的函数是返回一个值,或者true等等等等,现在是return $this;直接返回自身(不知道这样效率是提升还是下降。),但总体来说,这个还是一个很不错的方法。

反正,自从我在Zend的FW里看到这个连贯接口后,接下来,在很多地方就看到了不同的介绍、应用。慢慢享受吧。HOHO

关于PHP正则的取反和逻辑思想的问题

关于PHP正则的取反和逻辑思想的问题

  众所周知,^这个符合在正则里非常有用,首先它代表了行首匹配,比如/^abc/,就代表了必须是abc开头,同样,它也是一个取反的标记。

  何为取反?就说刚才吧^abc是以abc开头,那么取反的意思就是不以abc三个字母的开头。

  正常情况下,这些都可以运用,不会有什么错误。

  再举个例子:我要判断字符串是否有特殊字符,怎么办?
  正常情况下,我会这样preg_match("/[_-~!@#$%^]+/",$string) ,当然特殊字符不止这么一些,只是举个简单的例子。
  如果我的$string = 'abcdefg--hijklmn',那么,我这样的匹配肯定是返回1的,因为有“-”被我匹配了。
  现在反过来讲,如果我不想匹配这些特殊字符怎么办?
  首先想到的就是取反,于是我preg_match("/[^-~!@#$%^]/",$string),再匹配一下,结果还是返回1。
  为啥呢?
  起初想半天就没想明白,为什么我明明取反了却匹配仍然成功呢?
  仔细思索了好久才明白,我取反之后,其实我的match的内容就变成了:与不是特殊字符匹配,那么我的string里面几乎全都不是特殊字符,当然就匹配成功了。
  所以,对于这种情况,还是先判断是否有特殊字符,然后再根据返回值再判断是否没有特殊字符吧。(有点象绕口令了)。

PHP常见错误(二)

PHP常见错误(二)

mySQL Result Source Errors
mySql 资源错误
Warning: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in…
You need to take a look at the $result variable used to define the loop. More than likely there is a syntax error on the reported line before the $result field, or the value of $result does not exist.
通常,在进行msyql_query后,你需要对取得的数据进行遍历,如果出现上面错误时,你得检查一下你的$result是否是一个resource,这种情况在SQL查询语句错误(但错误没有被抛出)的情况下,所以,发现此问题时,可以考虑打印出SQL,到PHPMYDMIN里执行一下看看是怎么回事。

Supplied argument is not a valid stream resource…
This is usually caused when your code is looking for a table or other resource in the database that does not exist.
这个问题的情况通常是你代码中指定的表或者资源不存在于该数据库

Sessions are not being created or maintained
Sessions 不能被创建或者被修改维护
This can apply to any of the scenarios below:
这可能会出现以下几种情况
(1) The program isn’t remembering your login 程序没有记录下你的登录状态
(2) Your shopping cart won’t hold items 你的购物车没有保持你的商品状态
(3) Your php script is redirecting like crazy 你的PHP代码跳转的太疯狂
(4) “Call undefined function session_start” error 你的服务器没有打开Session功能
(5) PHP isn’t processing pages called by something like index.php?page=home&id=7 你的PHP代码不能打开某个页面,比如index.php?page=home&id=7

Your site is most likely dependent on register_globals. You can enable them by putting this line in your .htaccess (or just enabling in your php.ini if you have access):
你的网站可能会依赖register_globals(全局变量)打开。你可以将下面这行加到你的.htaccess文件里(当然,如果你有权限更改PHP.ini,还是改php.ini更方便)
php_flag register_globals On

Stream Errors
文件流错误
Warning: failed to open stream…
Warning: main(/index.php): failed to open stream: No such file or directory in…

This is usually because either the specified file is missing, or a file declared in a require() or include() function is missing. The easiest way to fix this is by re-installing the PHP program from a freshly-uploaded copy, or restoring the original config.php and just changing the db information. The include path may also be incorrect, but either way your script is looking for a file that isn’t there, or it is looking in the wrong place.
这两个问题的出现,通常是文件没有找到或者文件不能被require和include。(上面的英文我觉得不太准,个人认为应该是:先检查一下文件是否确实存在,检查文件的路径,当然,还得看一下php.ini的include_path,如果这些都检查过了。那么尝试用绝对路径加载文件)

Warning: fopen(…): failed to open stream: Permission denied in…
This is a permissions and/or ownership issue. Try first setting the permissions to 777 just to see if the script will run. If so, you should narrow down the permissions to 775. If not, set the user/group to user:nobody.
文件没有权限被读取,如果你想要你的文件被读取,设置文件权限为775,如果该文件需要被执行,那么就设置成777。否则设置用户权限/组为:nobody

Warning: <…> is not a valid stream resource…
Warning: fread(): supplied argument is not a valid stream resource in…
This is an error seen when trying to use functions like fopen(), fread(), feof(), etc. and are usually caused by an invalid or unavailable resource that is being called in the line specified. For instance, if the fread() function is returning this error, it could be that the file it is trying to access does not have the correct permissions or does not exist.
这也是一个常见错误,当你使用fopen(), fread(), feof()等函数时,就可能会出现,错误原因一般是fopen()或者其他函数打开文件失败,但错误信息被屏蔽,如$fp=@fopen('file.txt');,在这种情况下,再使用fread()读取时,就会出现上面的出错信息。

Warning: Failed opening….
Warning: Failed opening ‘…’ for inclusion (include_path=’.:/usr/local/lib/php’) in Unknown on line 0
Make the sure that the file mentioned (and its holding directorie) has read + execute permissions, and that the path to the file is correct. If not, you’ll need to add the path into the PHP code: (or .htaccess)
请确认文件是否有读写权限并且路径是正确的。如果不是,你需要把路径加载到你的PHP代码或者.htaccess文件里

include(”/path/to/files”);
Blank PHP Pages

You go to a .php page, but it’s blank.
The scope of what can cause blank pages is very broad, but there are a few things to look at:
-Is error reporting turned off anywhere in the script or in the .htaccess? If so, turn it on to see what is happening (php_flag display_errors on), or add the lines at the top of this page into the script.
-Is the PHP script even generating any output (usually you can tell my finding the print function?
-Check the database connection, i.e, username, dbname, user added to db, etc.
-Try using the full <?PHP ?> tags, rather than the shorter versions <? ?>
当你转向到a.php的页面时,但是打开的却是空白页面,出现这种问题的原因有很多,这里只介绍一些让你尝试着解决一下:
1、错误报告是否关闭了?如果是,请打开错误报告,并且在你的这个文件里加上一些输出试试
2、是否PHP代码本身就是一个输出页面,试着寻找一下print之类的函数?
3、检查数据库连接,例如,用户名密码等
4、试着用完整的PHP标签:<?php?>,而尽量少用短标签<??>


Also, if the software is prebundled (like phpBB or Gallery), then the index or one of the include pages could be corrupted. Usually you can just replace the problem page with a working version from another installation.
有时,一些软件的测试版本中往往都会存在着一个错误或者BUG等,你可以试着将出错页用该程序的其他版本来替换掉。象(phpBB或者Ballery)


Max Execution Time Error
最大执行时间错误
You receive some variant of a “Max_execution_time” error when loading a page.
This is caused when a PHP script takes longer to execute than the server allows, but can be adjusted by adding a PHP directive to your .htaccess: (in seconds, 0 = unlimited) or modifying the value in php.ini.
php_value max_execution_time 0
打开页面时,显示 "Max_execution_time" 执行时间错误。
这应该是你的PHP代码在运行的时候,超过了服务器所允许的时候,但是,这是可以通过修改.htaccess或者php.ini来进行修改(0代表了不限制)
php_value max_execution_time 0


Open_basedir Errors

Warning: Unknown(): open_basedir restriction in effect.
This is a protective feature of Apache that restricts PHP from accessing files/folders outside the user’s home directory. Most of the time this is due to an incorrect include path in one or more of the config files (which are usually mentioned). Look for something like this:
/includes/somefile.php
/admin/files/anotherfile.php
The heading / tells the filesystem that these folders are on the server root, and thus prevents PHP from accessing them. You can usually fix this by changing the path to these files to be absolute to their location:
/home/username/public_html/includes/somefile.php
or
./includes/somefile.php
出现Open_basedir问题的时候,往往需要先检查,PHP_INI里面有没有设定open_basedir,如果PHP.ini里没有设定,还需要查看apache的http.conf的虚拟主机设定里,也否也进行了设置。如果设定了,还需要检查这些目录是否可读写,否则会出现受保护的错误。

JpGraph中文乱码完美解决方案[转]

JpGraph中文乱码完美解决方案[转]

很多人使用JpGraph会出现乱码问题,但是网上有好多并无效果的解决方式,经过反复试验及在网上看了其它人的经验,总结出在2.3版下最终较完美解决方案
1、把simsun.ttc字体拷到服务器/usr/X11R6/lib/X11/fonts/truetype下,当然这个目录可以自定义
2、不需要更改$aFF === FF_SIMSUN或$aFF === FF_CHINESE ,直接用以下代码即可
$title = "JpGraph中文测试";
$title = iconv("UTF-8", "gb2312", $title);
$graph->title->Set($title);
就可以完美的解决中文问题了
3、还有一个问题,就是右上方的中文显示问题,就是SetLegend函数,解决方式如下
打开jpgraph.php文件,找到
private $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;
用
private $font_family=FF_SIMSUN,$font_style=FS_NORMAL,$font_size=8;
替换就OK了^_^

PHP常见错误的翻译(一)

PHP常见错误的翻译(一)

Common PHP Errors
常见PHP错误
December 7, 2007 - 10:44 pm | Nessa (来源,忘了从哪个网站COPY的了)
I’m going back to the basics here, you know, when you wrote your first PHP script and saw an ugly-ass error message pop up on your screen? Error messages are the best tool a programmer has.
我又回到最基础的上面来了,你知道,当你在最初写PHP代码时,你是否经常看到那些可恨(我翻译成可恨了)的出错信息?其实,出错信息对一个程序员来说是一个最好的工具。

Set up Error Reporting
设置出错报告

Most PHP errors are straight forward, but there are times where you don’t see any which makes it very difficult to tell what the problem is.
大多数PHP的错误信息都比较直观,但有时你看到的错误信息却很难明确的告诉你问题在哪里。

The first step of PHP troubleshooting is to turn error reporting on. For security reasons you’ll want error reporting off by default, but if something goes wrong you’ll need the information for debugging. You can usually enable error reporting by adding this line to the problem script:
首先你得打开你的 PHP出错报告。从安全角度来说,你可能会想到关闭它。但是如果程序出了问题,而你又需要debug的时候,你可以通过以下的PHP代码临时来打开出错信息。

<?php error_reporting(E_ALL) ?>

Or you can add these lines to the root .htaccess:
或者,你可以将以下几行加入到.htaccess文件里

php_flag display_errors on
php_value error_reporting 6143

This will usually display an error useful for troubleshooting, that is, if the software and your server configuration allows it.
如果软件或者服务器支持,这可以显示一些有用的错误信息

例如:
Parse Errors
解析错误
Parse error: parse error, unexpected T_STRING in……
This is a syntax error. Perhaps you forgot a semi-colon at the end of a line, or you forgot a double quote (”) or an end bracket (}) after you started one. For quote and semicolon issues, the problem is usually the line above the one reported in the error. For brackets, it may be at the end of the script.
这是一个语法构造错误。(这段翻译我想不出怎么好,自己根据理解翻译)出现这段信息的时候,往往都在出错代码的上一行,请检查一下代码的结束,是否双引号或者大括号不匹配,有时候也会有一些字符串的问题在里面。

Parse error: syntax error, unexpected $end in
You’re most likely missing a } somewhere. Make sure that each { you have is also closed with a }.
这东西是忘记了“}”,请检查“{}”是否一一对应了。

Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in..
There may be double quotes within double quotes. They either need to be escaped or brought to single quotes. It’s also possible that a new PHP statement was started before the previous was finished.
出现这种问题往往是在双引号中又有了双引号,可能需要在双引号前加上转义符或者使用单引号。通常问题在显示的行数之上一行。

Header Errors
头出输出错误

Warning: Cannot add header information - headers already sent by (output started at /home/vnessa5/www/errors.php:9) in….
Warning: Cannot send session cache limiter - headers already sent in somefile.php on line 222

Naturally, HTML will parse before PHP. The script is trying to send header information after you’ve already sent output to the browser. HTTP headers are required to be sent before any output from your script, which means that a header function must be placed before any html or even a white space. There are two solutions for this. Either (1) Set the header tags the top of the document, or (2) insert a header redirect by adding this to the very top of the page to force the output buffer:
一般情况下,HTML的解析都会在PHP前面,而代码尝试在发送header相关信息时,你已经有了HTML输出。所以你在发现有这种情况出现的时候,请检查你的代码以保证没有HTML代码或者甚至是一个空格。一般有两个解决方案。1)在文件头部设定HEADER标签。2)在你要输出的页面前插入HEADER标签的内容。

<?php ob_start();

Then this at the very end of the page (not usually required)

ob_end_flush(); ?>

快速生成目录树

快速生成目录树

网页开发时,很多时候都会遇到生成目录树的问题,系统自带的mkdir仅仅只能生成单个目录,当面对“abc/def/ghi/jkl/”这样的路径时,就没有办法了。这时候,可以利用一个简单的递归,同时调用PHP自带函数dirname,就可以很方便的生成目录树

<?php

function mkdirs($path , $mode = 0755 ){
if(!is_dir($path)){
mkdirs(dirname($path),$mode);
mkdir($path,$mode);
}
return true;
}

这样就比较方便的生成一个目录树了,该函数主要用于:1、生成缓存目录,2、生成静态文件目录,3、生成上传文件目录。

其它地方用的并不是很多