PHP 中文处理技巧

 更新时间:2016年11月25日 15:57  点击:1836
网上的PHP端escape unescape函数建议不要用,它把中英文混合时的英文过滤掉了,我是莫名其妙了N久啊,建议用unicode_urldecode这个。


function unicode_urldecode($url)
{
preg_match_all('/%u([[:alnum:]]{4})/', $url, $a);
foreach ($a[1] as $uniord)
{
$dec = hexdec($uniord);
$utf = '';
if ($dec < 128)
{
$utf = chr($dec);
}
else if ($dec < 2048)
{
$utf = chr(192 + (($dec - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
}
else
{
$utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
$utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
}
$url = str_replace('%u'.$uniord, $utf, $url);
}
return urldecode($url);
}

再就是编码格式的转换,这主要涉及数据的存储和客户端返回,用iconv就搞定,这个函数似乎是从C++中借鉴来的

类只能php教程 5.30以上的版本才能使用,继承了上一个版本的快速重定向的特点(单独类,全部使用静态调用),增添了一个很重要的功能和属性 可以调用其他url中的模块了 也使得模块与模块间或页面与页面间的函数简化共享得以实现

.htaccess文件写法:
复制代码 代码如下:
#-------------- .htaccess start ---------------

RewriteEngine on
RewriteRule !.(js|ico|gif|jpg|png|css教程|swf|htm|txt)$ index.php
php_flag magic_quotes_gpc off
php_flag register_globals off

#-------------- .htaccess end ---------------

 

重写功能引入:让站点根目录的index.php末尾写上下列代码,重写就开启了(正常条件:1.apache的重写配置成功,且开启了.htaccess支持的.2.站点根目录的.htaccess文件设置好了.3.class.rewrite.php类文件在index.php前面部分加载了.4.页面模块文件位置及写法无误):
复制代码 代码如下:
//............

Rewrite::__config(
$config['path'],/*'http://xxxxx/mysite/'URL基础位置*/
$config['md_path'],/*'c:/phps教程ite/www/mysite/modules/'模块文件物理目录*/
array(
'phpinfo'
)
);
Rewrite::__parse();

//..........

模块文件写法:

testPk.php
复制代码 代码如下:
<?php
class Rw_testPk extends Rewrite {

//这个是前导函数,只要访问到testpk这个页面,这个必然会执行,可用来控制本页面内函数访问权限或本页面全局变量
public static function init(){
//if (!defined('SITE_PASS')){
echo self::$linktag.'<br/>';//self::$linktag是页面解析位置路径值,会常使用.
//}
}

//当访问"http://localhost/testpk/"时会执行
public static function index(){
echo 'test';
}

//当访问"http://localhost/testpk/blank"时会执行或写作"http://localhost/testpk/index/blank"一般"index/"都是可以被省略的
public static function blank(){}
}
?>

class.rewrite.php;
复制代码 代码如下:
<?php

class Rewrite{

public static $debug = false;//是否打开调试
public static $time_pass = 0;//获得模块文件整体执行时间
public static $version = 2.2;
public static $pretag = 'Rw_';//模块文件类的名称前缀

public static $linktag = 'index';//页面链接标记,用来标记解析的是那个链接,可用来控制各种菜单效果和链接访问权限

protected static $time_start = 0;
protected static $time_end = 0;
protected static $physical_path = '';//模块文件的物理路径
protected static $website_path = '';//模块文件的站点路径,因为可能把站点放大站点的子目录下,如:http://localhost/site/mysite
protected static $ob_contents = '';
protected static $uid = 0;//配合个人主页访问方式 如http://localhost/423/则是访问http://localhost/profile?uid=423

//允许的系统函数如$allow_sys_fun=array('phpinfo')那么系统将允许链接访问phpinfo内容了,当http://localhost/phpinfo或http://localhost/......./phpinfo时就会直接执行phpinfo这个函数,不需要phpinfo.php模块文件
private static $allow_sys_fun = array();

private static function __get_microtime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}


//设置调试Rewrite::__debug(true);
public static function __debug($d = true){
static::$debug = $d;
}

//配置路径和允许函数
public static function __config($website_path = '',$physical_path = '',$allow_sys_fun = array()){
self::$physical_path = $physical_path;
self::$website_path = $website_path;
self::$allow_sys_fun = $allow_sys_fun;
}

//调试函数
public static function __msg($str){
if(static::$debug){
echo "n<pre>n".print_r($str,true)."n</pre>n";
}
}

//解析开始时间
public static function __start(){
self::$time_start = self::__get_microtime();
}

//解析结束时间
public static function __end($re = false){
self::$time_end = self::__get_microtime();
self::$time_pass = round((self::$time_end - self::$time_start),6) * 1000;
if($re){
return self::$time_pass;
}else{
self::__msg('PASS_TIME: '.self::$time_pass.' ms');
}
}

//内部跨模块url解析调用,如在test1.php模块页面中执行了Rwrite::__parseurl('/test2/show')这句,将调用test2.php模块页面中的show方法(Rw_test2这个class的方法)
public static function __parseurl($url = '',$fun = '',$data = NULL){
if(!empty($url)&&!empty($fun)){
$p = static::$physical_path;
if(file_exists($p.$url) || file_exists($p.$url.'.php') ){
$part = strtolower(basename( $p.$url , '.php' ));
static::$linktag = $part.'/'.$fun;
$fname = static::$pretag.$part;
if(class_exists($fname, false)){
if(method_exists($fname,$fun)){
return $fname::$fun($data);
}
}else{
include( $p.$url );
if( class_exists($fname, false) && method_exists($fname,$fun)){
return $fname::$fun($data);
}
}
}
}
}

//核心链接解析函数Rwrite::__parse();在顶级重写核心定向目标index.php中的执行,意味着Rwrite自定义重写开启
public static function __parse($Url = ''){
self::__start();
$p = static::$physical_path;
$w = static::$website_path;
$req_execute = false;

$url_p = empty($Url) ? $_SERVER['REQUEST_URI'] : $Url;
$local = parse_url($w);
$req = parse_url($url_p);
$req_path = preg_replace('|[^w/.\]|','',$req['path']);
$req_para = empty($Url) ? strstr($_SERVER['SERVER_NAME'],'.',true) : 'www';
if(empty($Url) && substr_count($_SERVER['SERVER_NAME'],'.') == 2 && $req_para != 'www'){
self::__goto($req_para,preg_replace('|^'.$local['path'].'|',"",$req_path));
return ;
}else{
$req_path_arr = empty($req_path)?array():preg_split("|[/\]+|",preg_replace('|^'.$local['path'].'|',"",$req_path));
$req_fun = array_pop($req_path_arr);
if(substr($req_fun,0,2)=='__'){
$req_fun = substr($req_fun,2);
}
$req_path_rearr = array_filter($req_path_arr);

self::__msg($req_path_rearr);

$req_temp = implode('/',$req_path_rearr);
$fname = $req_temp.'/'.$req_fun;
if(!empty($req_fun)&&in_array($req_fun,static::$allow_sys_fun)){
$req_fun();
}else{
if(!empty($req_fun)&&file_exists($p.$fname.'.php') ){
include( $p.$fname.'.php' );
}else{
$fname = empty($req_temp) ? 'index' : $req_temp;
if(file_exists($p.$fname.'.php') ){
include( $p.$fname.'.php' );
}else{
$fname = $req_temp.'/index';
if(file_exists($p.$fname.'.php')){
include( $p.$fname.'.php' );
}else{

//这个地方是对"个人主页"的这种特殊链接定向到"profile/"了,可自己修改
//如:www.xxx.com/12/将表示www.xxx.com/profile/?uid=12或www.xxx.com/profile?uid=12


$uid = is_numeric($req_temp) ? $req_temp : strstr($req_temp, '/', true);
$ufun = is_numeric($req_temp) ? 'index' : strstr($req_temp, '/');
if(is_numeric($uid)){
self::$uid = $uid;
if(!isset($_GET['uid'])) $_GET['uid'] = $uid;
$fname = 'profile/'.$ufun;
if(file_exists($p.$fname.'.php')){
include( $p.$fname.'.php' );
}else{
header("location:".$w);
exit();
}
}else if(file_exists($p.'index.php')){
$fname = 'index';
include( $p.'index.php' );
}else{
header("location:".$w);
exit();
}
}
}
}
$ev_fname = strrpos($fname,'/')===false ? $fname : substr($fname,strrpos($fname,'/')+1);
$ev_fname = static::$pretag.$ev_fname;
if( class_exists($ev_fname, false) && method_exists($ev_fname,$req_fun)){
static::$linktag = $req_fun=='index' ? $fname.'/' : $fname.'/'.$req_fun;
if($req_fun != 'init' && method_exists($ev_fname,'init')){
$ev_fname::init();
}
$ev_fname::$req_fun();
}else if( class_exists($ev_fname, false) && method_exists($ev_fname,'index') ){
static::$linktag = $fname.'/';
if(method_exists($ev_fname,'init')){
$ev_fname::init();
}
$ev_fname::index();
}else if( $fname != 'index' && class_exists(static::$pretag.'index', false) && method_exists(static::$pretag.'index','index') ){
$ev_fname = static::$pretag.'index';
static::$linktag = 'index/';
if(method_exists($ev_fname,'init')){
$ev_fname::init();
}
$ev_fname::index();
}else{
self::__msg('Function Not Exist!');
}
}
}
self::__end();
}

//这里是用户自定义链接的解析(用数据库教程存储的解析值) 如: xiaoming.baidu.com

//数据库中 xiaoming这个标签指向一个人的博客 就会到了www.baidu.com/blog?uid=12或www.baidu.com/blog?uname=xiaoming(这个就看自己咋设计数据库了)

public static function __goto($para = '',$path = ''){
$w = static::$website_path;
if(empty($para)){
exit('未知链接,解析失败,不能访问');
}
if(class_exists('Parseurl')){
$prs = Parseurl::selectone(array('tag','=',$para));
self::__msg($prs);
if(!empty($prs)){
$parastr = $prs['tag'];
$output = array();
$_GET[$prs['idtag']] = $prs['id'];
parse_str($prs['parastr'], $output);
$_GET = array_merge($_GET,$output);
$path = $prs['type'].'/'.preg_replace('|^/'.$prs['type'].'|','',$path);
self::__msg($path);
header('location:'.$w.$path.'?'.http_build_query($_GET));
exit();
}else{
header("location:".$w);
exit();
}
}else{
header("location:".$w);
exit();
}
}
}
?>

$list = array(
        array('id'=>107,'title'=>'aaa'),
        array('id'=>106,'title'=>'bbb'),
        array('id'=>105,'title'=>'ccc'),
        array('id'=>104,'title'=>'ccc'),
        array('id'=>103,'title'=>'ddd'),
        array('id'=>102,'title'=>'eee'),
        array('id'=>101,'title'=>'fff'),
       
        );


function assoc_title($arr, $key) {
                        $tmp_arr = array();
                        foreach($arr as $k => $v) {
                        if(in_array($v[$key], $tmp_arr)) {
                                unset($arr[$k]);
                        } else {
                                $tmp_arr[] = $v[$key];
                        }
                }
                        return $arr;
}//assoc_title end
$key_title='title';
assoc_title($list, $key_title);
print_r($list);

/*
 方法二用 array_unique()
 array_unique() 函数移除数组中的重复的值,并返回结果数组。 当几个数组元素的值相等时,只保留第一个元素
 
*/
$sarray = array('0','111cn.net','www.111cn.net','0');
$s = array_unique($sarray);
print_r($s);

/*
得出结果为
Array
(
    [0] => 0
    [1] => 111cn.net
    [2] => www.111cn.net
)
*/

 

简单说一下就是
-> 调用对象的成员
=> 指定下标

下面来看几个实例关于-> 与=>代码,让你更清楚是什么用法与功能并且有什么区别了。
*/
// =>用法,可用到数组array,赋值与foreach赋值

$array = array("addr" => "www.111cn.net","tel" => "11111111");

print_r($array);

/*
结果为
Array
(
    [addr] => www.111cn.net
    [tel] => 11111111
)
*/

//用来foreach实例

foreach( $array as $v =>$_v )
{
 echo $_v,'<br />';
}

/*
结果
www.111cn.net
11111111

*/

// ->用法上面说了是用于对象的,那我们就来做个实验

class user{
 var $url ='www.111cn.net';
 
 public function showUrl()
 {
  echo $this->url;
 }
}

//调用 user类方法

$u = new user();
$u->showUrl();

/*
输出结果为 www.111cn.net

本文章原创于www.111cn.net[中国WEB第一站]转载注明来源

class cacheClearFile{
 
 var $dir = '111cn.Net'; 
 
 function __construct()
 {
  $this->listFils(); 
 }  

  
 function listFils()
 {
  if(is_dir($this->dir))
  { 
   if($dir_file=opendir($this->dir))
   {
    while(($dir_list=readdir($dir_file))!==false)
    {
     if($dir_list!="." && $dir_list!="..")
     {
      if( is_file($dir_list)
      {
       unlink($dir_list);
      }
      else
      {
       $this->dir =$dir_list;
       listFils();
      }     
     }
    }
   }else{
    echo("目录不能打开");
   }
  }
  else
  {
   echo("不是目录");
  }
 }
}

//实例调用方法

new cacheClearFile();

/*
只用了一句就OK了,因为我们用了构造函数所以只在创建类他就会自动给执行了。
本站原创文章转载注明出处 www.111cn.net 中国WEB第一站

[!--infotagslink--]

相关文章

  • js URLdecode()与urlencode方法支持中文解码

    下面来介绍在js中来利用urlencode对中文编码与接受到数据后利用URLdecode()对编码进行解码,有需要学习的机友可参考参考。 代码如下 复制代码 ...2016-09-20
  • photoshop打开很慢怎么办 ps打开慢的设置技巧

    photoshop软件是一款专业的图像设计软件了,但对电脑的要求也是越高越好的,如果配置一般打开ps会比较慢了,那么photoshop打开很慢怎么办呢,下面来看问题解决办法。 1、...2016-09-14
  • Windows批量搜索并复制/剪切文件的批处理程序实例

    这篇文章主要介绍了Windows批量搜索并复制/剪切文件的批处理程序实例,需要的朋友可以参考下...2020-06-30
  • Jquery Ajax Error 调试错误的技巧

    JQuery使我们在开发Ajax应用程序的时候提高了效率,减少了许多兼容性问题,我们在Ajax项目中,遇到ajax异步获取数据出错怎么办,我们可以通过捕捉error事件来获取出错的信息。在没给大家介绍正文之前先给分享Jquery中AJAX参...2015-11-24
  • BAT批处理判断服务是否正常运行的方法(批处理命令综合应用)

    批处理就是对某对象进行批量的处理,通常被认为是一种简化的脚本语言,它应用于DOS和Windows系统中。这篇文章主要介绍了BAT批处理判断服务是否正常运行(批处理命令综合应用),需要的朋友可以参考下...2020-06-30
  • 关于Mysql中文乱码问题该如何解决(乱码问题完美解决方案)

    最近两天做项目总是被乱码问题困扰着,这不刚把mysql中文乱码问题解决了,下面小编把我的解决方案分享给大家,供大家参考,也方便以后自己查阅。首先:用show variables like “%colla%”;show varables like “%char%”;这两条...2015-11-24
  • C#读取中文文件出现乱码的解决方法

    这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • php语言中使用json的技巧及json的实现代码详解

    目前,JSON已经成为最流行的数据交换格式之一,各大网站的API几乎都支持它。我写过一篇《数据类型和JSON格式》,探讨它的设计思想。今天,我想总结一下PHP语言对它的支持,这是开发互联网应用程序(特别是编写API)必须了解的知识...2015-10-30
  • Windows服务器MySQL中文乱码的解决方法

    我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15
  • Mysql在debian系统中不能插入中文的终极解决方案

    在debian环境下,彻底解决mysql无法插入和显示中文的问题Linux下Mysql插入中文显示乱码解决方案mysql -uroot -p 回车输入密码进入mysql查看状态如下:默认的是客户端和服务器都用了latin1,所以会乱码。解决方案:mysql>use...2013-10-04
  • 图解Sublime Text3使用技巧

    通过本篇文章给大家介绍Sublime Text3使用技巧的相关知识,对sublime text3技巧相关知识感兴趣的朋友一起学习吧...2015-12-24
  • linux mint 下mysql中文支持问题

    一.mysql默认不支持中文,它的server和db默认是latin1编码.所以我们要将其改变为utf-8编码,因为utf-8包含了地球上大部分语言的二进制编码 1.关闭mysql服务 sudo /etc/init.d/mysql stop 2.修改mysql配置文件 mysql配...2015-10-21
  • PHP file_get_contents设置超时处理方法

    file_get_contents的超时处理话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。今天说的这篇是讲超时的,确实在...2013-10-04
  • php怎么用拼音 简单的php中文转拼音的实现代码

    小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
  • 基于PHP给大家讲解防刷票的一些技巧

    刷票行为,一直以来都是个难题,无法从根本上防止。但是我们可以尽量减少刷票的伤害,比如:通过人为增加的逻辑限制。基于 PHP,下面介绍防刷票的一些技巧:1、使用CURL进行信息伪造$ch = curl_init(); curl_setopt($ch, CURLOP...2015-11-24
  • C#多线程中的异常处理操作示例

    这篇文章主要介绍了C#多线程中的异常处理操作,涉及C#多线程及异常的捕获、处理等相关操作技巧,需要的朋友可以参考下...2020-06-25
  • postgresql 中的时间处理小技巧(推荐)

    这篇文章主要介绍了postgresql 中的时间处理小技巧(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
  • Java连接数据库oracle中文乱码解决方案

    这篇文章主要介绍了Java连接数据库oracle中文乱码解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-16
  • Python同时处理多个异常的方法

    这篇文章主要介绍了Python同时处理多个异常的方法,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-29
  • 分享12个非常实用的JavaScript小技巧

    这篇文章主要介绍了分享12个非常实用的JavaScript小技巧,这些小技巧可能在你的实际工作中或许能帮助你解决一些问题,需要的朋友可以参考下...2016-05-14