PHP处理csv文件 以及处理中文会遇到的问题

今天要处理一个excel文件 其实用php的一个库PHPexcel那个库就可以 但是我懒得还要下个库 就把excel先转换成csv,因为php里面有内置的函数可以直接支持csv,但是处理中文的时候会遇到乱码或者中文缺失的问题 下面这段源代码就显示怎样读取数据并怎样添加到数组中

//页面用utf-8输出 避免乱码
header('Content-Type: text/html; charset=utf-8');
//设置地区 解决中文缺失的问题
setlocale(LC_ALL,'zh_CN.utf8');
//打开csv文件
if (($handle = fopen("sichuan.csv", "r")) !== FALSE) {
	$ads = array();
	$id = 0;
    //处理数据
    while (($data = fgetcsv($handle,1000,",")) !== FALSE) {
    	$ads[$id]['id'] = $id;
    	$ads[$id]['name'] = $data[5];
    	$ads[$id]['pdps'] = '';
    	$id++;
    }
    //可以处理成json
    $str = json_encode($ads);
    echo $str;
    fclose($handle);
}

PHP处理xml的方法

好久不用php了 这几天用php做一个小东西 处理xml是一个很基本的常识  总结了一下 以后用的时候找起来方便

其实就是利用了一个php内置的方法simplexml_load_string();下面用直接上代码说的清楚,我用了dict.cn的辞典API,准备做一个简单的辞典小应用~ 嘻嘻

	function object_to_array($object){
        $result = array();
        $object = is_object($object) ? get_object_vars($object) : $object;
        if(empty($object) || (!is_object($object) && !is_array($object))){
			return false;
		}
		foreach ($object as $key => $val) {
		    $val = (is_object($val) || is_array($val)) ? object_to_array($val) : $val;
		    if(is_numeric($val) || is_string($val))
    		{
				$val = mb_convert_encoding($val,"GB2312","UTF-8");
    		}
    		$result[$key] = $val;
        }
        return $result;
    }

        $word = $_GET['word'];
	$str = file_get_contents('http://api.dict.cn/ws.php?utf8=true&q='.$word);
	$obj = simplexml_load_string($str);
	$arr = object_to_array($obj);
	var_dump($arr);

过程就是先把字符串读过来 然后把字符串用内置方法转成对象 然后再把对象 用自定义的函数 转成数组
我们来看一下他的结果把~

xml to array

让json_encode支持中文

json_encode,发现它不支持中文,数组中所有中文在json_encode之后都不见了或者出现\u2353等。
在json_encode之前,把所有数组内所有内容都用urlencode()处理一下,然用json_encode()转换成json字符串,最后再用urldecode()将编码过的中文转回来。
那么应该怎么办那 下面是两种方法

/**************************************************************
 *
 *	使用特定function对数组中所有元素做处理
 *	@param	string	&$array		要处理的字符串
 *	@param	string	$function	要执行的函数
 *	@return boolean	$apply_to_keys_also		是否也应用到key上
 *	@access public
 *
 *************************************************************/
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            arrayRecursive($array[$key], $function, $apply_to_keys_also);
        } else {
            $array[$key] = $function($value);
        }

        if ($apply_to_keys_also && is_string($key)) {
            $new_key = $function($key);
            if ($new_key != $key) {
                $array[$new_key] = $array[$key];
                unset($array[$key]);
            }
        }
    }
}
/**************************************************************
 *
 *	将数组转换为JSON字符串(兼容中文)
 *	@param	array	$array		要转换的数组
 *	@return string		转换得到的json字符串
 *	@access public
 *
 *************************************************************/
function JSON($array) {
	arrayRecursive($array, 'urlencode', true);
	$json = json_encode($array);
	return urldecode($json);
}

第二种方法很简单 但是比较难看懂 用正则和iconv

$code = json_encode($str);
$json = preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $code);

smarty mini memo

Smarty 中的 if 语句和 php 中的 if 语句一样灵活易用,并增加了几个特性以适宜模板引擎. if 必须于 /if 成对出现. 可以使用 else 和 elseif 子句. 可以使用以下条件修饰词:eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、==、!=、>、<、<=、>=. 使用这些修饰词时必须和变量或常量用空格格开.

equal : 相等、not equal:不等于、greater than:大于、less than:小于、less than or equal:小于等于、great than or equal:大于等于、is even:是偶数、is odd:是奇数、is not even:不是偶数、is not odd:不是奇数、not:非、mod:取余、div by:被。。。除 and:与 or:或

apache rewrite入门

How it Works

Static URLs are known to be better than Dynamic URLs because of a number of reasons
1. Static URLs typically Rank better in Search Engines.
2. Search Engines are known to index the content of dynamic pages a lot slower compared to static pages.
3. Static URLs are always more friendlier looking to the End Users.
Open rewrite in your apache conf

Checkout the following lines

#line1

Options FollowSymLinks
AllowOverride All
Allow from all

#2
LoadModule rewrite_module modules/mod_rewrite.so

make sure the options is followsyslinks ,allowoverride is all and rewrite_module is loaded. Then make a file named .htaccess and copy it in your website root folder.
you should know some basic rules about apache rewrite, here’s some clips i quote from the Doc

文本
. 任意一个单字符
[chars] 字符类: “chars”中的任意一个字符
[^chars] 字符类: 不在”chars”中的字符
text1|text2 选择: text1 或 text2

量词
? 前面的字符出现 0 或 1 次
* 前面的字符出现 0 或 N 次(N > 0)
+ 前面的字符出现 1 或 N 次(N > 1)

分组
(text) text 组
(常用于设置一个选择的边界,或用于生成后引用:
在RewriteRule中可以用 $N 引用第N个分组)


^ 锚定到行首
$ 锚定到行尾

转义
\c 对给定的字符c进行转义
(比如对”.[]()”进行转义,等等)

for more info you can visit the offical doc , http://lamp.linux.gov.cn/Apache/ApacheMenu/mod/mod_rewrite.html

than here is a simple example:
if you want this url http://www.widgets.com/product.php?categoryid=1 convert to http://www.widgets.com/product/categoryid/(Any Value)/
copy the following code in your .htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteRule product/categoryid/(.*)/ product.php?categoryid=$1
RewriteRule product/categoryid/(.*) product.php?categoryid=$1

now the new url should work, that’s all,folks~