php hexdec()与dechex()十六进制转换为十进制互换
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(); //恢复错误信息指针
相关文章
- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- 这篇文章主要介绍了C# 16 进制字符串转 int的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 在串口通讯过程中,经常要用到 16进制与字符串、字节数组之间的转换...2020-06-25
- c#下进制互转代码...2020-06-25
- <?php教程 /** *十进制转二进制、八进制、十六进制 不足位数前面补零* * * @param array $datalist 传入数据array(100,123,130) * @param int $bin 转换的...2016-11-25
- 这篇文章主要介绍了C#十六进制字符串转十进制int的方法,涉及C#操作数制转换的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 下面小编就为大家带来一篇protobuf对象二进制序列化存储(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
php颜色转换函数hex-rgb(将十六进制格式转成十进制格式)
复制代码 代码如下:<?php function hex2rgb($colour) { if ($colour [0] == '#') { $colour = substr ( $colour, 1 ); } if (strlen ( $colour ) == 6) { list ( $r, $g,...2013-10-04- 这篇文章主要给大家介绍了C++中输出十六进制形式的字符串,文中给出了详细的介绍,有需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
- 这篇文章主要介绍了javascript常见数字进制转换,结合实例形式分析了JavaScript十进制,十六进制及二进制的相互转换原理与技巧,需要的朋友可以参考下...2016-04-23
- 这篇文章主要介绍了汇编语言进制转换之16进制转10进制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-08-16
- 在当前目录创建一个文件myfile.txt,对该文件具有读写权限...2020-06-25
- 这篇文章主要介绍了C#中图片、二进制与字符串的相互转换方法,涉及C#针对不同数据类型的解析与转换操作技巧,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了Python如何读写二进制数组数据,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-08-02
- 这篇文章介绍了C++ 十进制转换为二进制的实例代码,有需要的朋友可以参考一下...2020-04-25
- 这篇文章主要介绍了C#实现的基于二进制读写文件操作,结合具体实例形式分析了C#以二进制文件流形式针对文件进行读写操作的相关技巧,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了详解C++中十六进制字符串转数字(数值)的相关资料,这里提供两种实现方法,需要的朋友可以参考下...2020-04-25
- #中十六进制字符串的转换函数...2020-06-25
- 这篇文章主要介绍了C++二进制翻转,通过几个实例分析二进制翻转算法的实现技巧,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了C语言用栈实现十进制转换为二进制的方法,结合实例形式分析了C语言栈的定义及进制转换使用技巧,需要的朋友可以参考下...2020-04-25