php hexdec()与dechex()十六进制转换为十进制互换

 更新时间:2016年11月25日 17:40  点击:1497

hexdec() 函数把十六进制转换为十进制。

语法
hexdec(hex_string)

echo hexdec('77');       //输出119
echo "<br>";
echo hexdec(dechex(43));      //输出43
echo "<br>";
echo hexdec('3a');       //输出58


dechex() 函数把十进制转换为十六进制。

语法
dechex(dec_number)

function utf8_gb2312($str, $default = 'gb2312')
{
    $str = preg_replace("/[x01-x7f]+/", "", $str);
    if (empty($str)) return $default;
   
    $preg =  array(
        "gb2312" => "/^([xa1-xf7][xa0-xfe])+$/", //正则判断是否是gb2312
        "utf-8" => "/^[x{4e00}-x{9fa5}]+$/u",      //正则判断是否是汉字(utf8编码的条件了),这个范围实际上已经包含了繁体中文字了
    );

    if ($default == 'gb2312') {
        $option = 'utf-8';
    } else {
        $option = 'gb2312';
    }

    if (!preg_match($preg[$default], $str)) {
        return $option;
    }
    $str = @iconv($default, $option, $str);
   
    //不能转成 $option, 说明原来的不是 $default
    if (empty($str)) {
        return $option;
    }

默认编码是gb2312,而且我统计了一下,90%的情况下都是gb2312,所以,我的检测函数不能出现本来是gb2312的,结果被检测出utf8. 基本思路是:

    1. 把所有的ascii去掉,如果全部都是ascii,那么就是gb2312。

    2. 假设这个字符串是gb2312,用一个正则检查它是否是真的gb2312,如果不是,那么就是utf-8

    3. 然后,用iconv 把字符串转换成utf8,如果转换不成功,那么原来可能不是真正的一个gb2312编码的字符

     (用正则匹配我已经尽量精确,但是,gb2312的编码不是连续的,还是会有空洞),那么最后的编码就是utf-8.

    4. 否则就是gb2312 编码

 加入这样的检查功能后,在1000个关键字里面,就出现了1个乱码,比以前的近100个关键字乱码少了很多。

 

void var_dump ( mixed expression [, mixed expression [, ...]])


此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值。数组将递归展开值,通过缩进显示其结构。

*/
function a_test($str)         //自定义函数
{
  echo "nhi: $str";         //输出参数
  var_dump(debug_backtrace());      //输出backtrace
}
a_test('friend');          //调用用户自定义函数

//再来看一个var_dump遍历对象实例吧

class foo {
private $a;
public $b = 1;
public $c='111cn.net';
private $d;
static $e;
public function test() {
var_dump(get_object_vars($this));
}
}
$test = new foo;
var_dump(get_object_vars($test));
$test->test();

//注意:为了防止程序直接将结果输出到浏览器,可以使用输出控制函数来捕获此函数的输出,并把它们保存到一个例如 string 类型的变量中。

include 'include.php教程';    //引用include.php文件

echo a();

//结果 bb
?>

include.php文件如下

<?php
//建立include.php以供其他文件调用
function a()     //定义函数a
{
  b();      //在函数a中调用函数b
}
function b()     //定义函数b
{
  c();      //在函数b中调用函数c
}
function c()     //定义函数c
{
  echo('bb');  //输出一个php backtrace
}
a();       //使用函数a
?>
其它提示:

在使用require()语句调用文件时,如果没有找到文件,require()语句会输出错误信息,并且立即终止脚本的处理.而include()语句在没有找到文件时则会输出警告,不会终止脚本的处理

定义和用法
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();         //恢复错误信息指针

[!--infotagslink--]

相关文章