首页 > 编程技术 > php

php正则匹配gb2312和utf-8中文

发布时间:2016-11-25 17:40

<?php教程
$action = trim($_get['action']);
if($action == "sub")
{
$str = $_post['dir'];
//if(!preg_match("/^[".chr(0xa1)."-".chr(0xff)."a-za-z0-9_]+$/",$str)) //gb2312汉字字母数字下划线正则表达式
if(!preg_match("/^[x{4e00}-x{9fa5}a-za-z0-9_]+$/u",$str)) //utf-8汉字字母数字下划线正则表达式
{
      echo "<font color=red>您输入的[".$str."]含有违法字符</font>";
}
else
{
       echo "<font color=green>您输入的[".$str."]完全合法,通过!</font>";
}
}
?>

定义和用法
error_reporting() 设置 php 的报错级别并返回当前级别。

语法
error_reporting(report_level)如果参数 level 未指定,当前报错级别将被返回。下面几项是 level 可能的值

*/
//关闭所有的错误报告
error_reporting(0);
//只报告运行错误
error_reporting(e_error|e_warning|e_parse);
//报告e_notice
error_reporting(e_error|e_warning|e_parse|e_notice);
//报告所有的运行错误,除了e_notice
//这是php.ini的默认值
error_reporting(e_all ^ e_notice);
//报告所有的php错误
error_reporting(e_all);
//和error_reporting(e_all)有一样的功效,该设置也会报告所有php错误
ini_set('error_reporting', e_all);

/*

值 常量 描述
1 e_error fatal run-time errors. errors that can not be recovered from. execution of the script is halted
2 e_warning non-fatal run-time errors. execution of the script is not halted
4 e_parse compile-time parse errors. parse errors should only be generated by the parser
8 e_notice run-time notices. the script found something that might be an error, but could also happen when running a script normally
16 e_core_error fatal errors at php startup. this is like an e_error in the php core
32 e_core_warning non-fatal errors at php startup. this is like an e_warning in the php core
64 e_compile_error fatal compile-time errors. this is like an e_error generated by the zend scripting engine
128 e_compile_warning non-fatal compile-time errors. this is like an e_warning generated by the zend scripting engine
256 e_user_error fatal user-generated error. this is like an e_error set by the programmer using the php function trigger_error()
512 e_user_warning non-fatal user-generated warning. this is like an e_warning set by the programmer using the php function trigger_error()
1024 e_user_notice user-generated notice. this is like an e_notice set by the programmer using the php function trigger_error()
2048 e_strict run-time notices. php suggest changes to your code to help interoperability and compatibility of the code
4096 e_recoverable_error catchable fatal error. this is like an e_error but can be caught by a user defined handle (see also set_error_handler())
8191 e_all all errors and warnings, except level e_strict (e_strict will be part of e_all as of php 6.0)

*/

function unserialize_handler($errno,$errstr)     //自定义函数
{
  echo "invalid serialized value.n";       //输出指定内容
}
$serialized='foo';          //定义字符串
set_error_handler('unserialize_handler');      //设置用户自定义错误信息函数
$original=unserialize($serialized);       //从已存储的表示中创建php的值
restore_error_handler();         //恢复错误信息指针

utf8_encode() 函数把 iso-8859-1 字符串编码为 utf-8。

utf8_encode(string);
*/
$str="你好,世界!";        //定义字符串
$result=utf8_decode($str);      //进行编码转换
echo $result;         //输出转换结果


//实例二

/*
utf8_decode() 函数把 utf-8 字符串解码为 iso-8859-1。

该函数把用 utf-8 方式编码的 iso-8859-1 字符串转换成单字节的 iso-8859-1 字符串。

如果成功,该函数将返回解码字符串;否则返回 false。

utf8_decode(string)

*/

$str="hello world!";        //定义字符串
$result=utf8_decode($str);      //进行编码转换
echo $result;
$result=utf8_encode($result);      //进行编码转换
echo $result;         //输出转换结果
?>

session_cache_limiter() 返回当前缓存限制的名字. 如果指定了 cache_limiter, 当前的缓存限制的名字被改为新值. 缓存限制控制着 HTTP 头发送到客户端的缓存控制. 这些确定页面内容规则的头内容可以被缓存.如果设置缓存设置为没有缓存(nocache), 将不允许任何客户端缓存. 但是公共变量可以允许缓存. 他也可以设置为私有的,这个比公共的多一点限制.

缓存显示在请求开始时被重新设置为 session_cache_limiter  的默认值.这样,你需要在每次请求时调用 session_cache_limiter() for every request (在 session_start() 调用前).

设置cache限制为'private'
*/

session_cache_limiter('private');
$cache_limiter=session_cache_limiter();
/*设置session的过期时间为30秒*/
session_cache_expire(30);
$cache_expire=session_cache_expire();
/*初始化session*/
session_start();
/*输出结果内容*/
echo "当前的session cache限制被设置为:$cache_limiter<br />";
echo "当前的session过期时间为:$cache_expire minutes";
/*
输出结果为:
the cache limiter is now set to private
the cached session pages expire after 30 minutes


*/

//实例二

/*设置caceh限制者为'private'*/
session_cache_limiter('private');
/*返回caceh限制者*/
$cache_limiter=session_cache_limiter();
echo "当前的session cache限制被设置为:$cache_limiter<br />";


//实例三

$filename="test.mpeg";
$filepath="test.mpeg";
session_start();
/*初始化session*/
session_commit();
/*输出请求的文件*/
header("content-type: audio/x-mpeg");  //或者其他类型的文件
header("content-disposition:attachment;filename=".$filename);
header("content-length:".$filesize);
header("content-transfer-encoding:binarynn");
header("pragma:no-cache");
header("expires:0");
$file_contents=file_get_contents($filepath);
print($file_contents

);

escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。

语法
escape(string)参数 描述
string 必需。要被转义或编码的字符串。

返回值
已编码的 string 的副本。其中某些字符被替换成了十六进制的转义序列

function php教程escape($str)
{
         $sublen=strlen($str);
         $retrunstring="";
         for ($i=0;$i<$sublen;$i++)
         {
                  if(ord($str[$i])>=127)
                  {
                           $tmps教程tring=bin2hex(iconv("gb2312","ucs-2",substr($str,$i,2)));
                           //$tmpstring=substr($tmpstring,2,2).substr($tmpstring,0,2);window下可能要打开此项
                           $retrunstring.="%u".$tmpstring;
                           $i++;
                  } else {
                           $retrunstring.="%".dechex(ord($str[$i]));
                  }
         }
         return $retrunstring;


unescape() 函数可对通过 escape() 编码的字符串进行解码。

语法
unescape(string)参数 描述
string 必需。要解码或反转义的字符串。

返回值
string 被解码后的一个副本。

说明
该函数的工作原理是这样的:通过找到形式为 %xx 和 %uxxxx 的字符序列(x 表示十六进制的数字),用 unicode 字符 u00xx 和 uxxxx 替换这样的字符序列进行解码。

php处理

function unescape($str) {
         $str = rawurldecode($str);
         preg_match_all("/%u.{4}|&#x.{4};|&#d+;|.+/u",$str,$r);
         $ar = $r[0];
         foreach($ar as $k=>$v) {
                  if(substr($v,0,2) == "%u")
                           $ar[$k] = iconv("ucs-2","gbk",pack("h4",substr($v,-4)));
                  elseif(substr($v,0,3) == "&#x")
                           $ar[$k] = iconv("ucs-2","gbk",pack("h4",substr($v,3,-1)));
                  elseif(substr($v,0,2) == "&#") {
                           $ar[$k] = iconv("ucs-2","gbk",pack("n",substr($v,2,-1)));
                  }
         }
         return join("",$ar);
}

标签:[!--infotagslink--]

您可能感兴趣的文章: