php实现的任意进制互转类分享,php实现进制分享
之前不知道php自带有base_convert可以实现任意进制之间的转换,自己写了一个。。。。
<?php /** * 进制转换类 * @author sgf@funcity * @version 2011-02-15 */ Class Hex{ private static $element = array( '0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F','G','H','I','J', 'K','L','M','N','O','P','Q','R','S','T', 'U','V','W','X','Y','Z' ); private static $hex_min = 2; private static $hex_max = 36; /** * 进制转换 */ public function conv($int,$out_hex,$in_hex=10,$use_system=true){ if($use_system && function_exists('base_convert')){ return strtoupper(base_convert($int,$in_hex,$out_hex)); } if($out_hex == $in_hex){ return $int; } if($out_hex > self::$hex_max || $out_hex < self::$hex_min){ return false; } if($in_hex > self::$hex_max || $in_hex < self::$hex_min){ return false; } $hex_10 = $this->_conv2hex10($int,$in_hex); return strtoupper($this->_conv_hex($hex_10,$out_hex)); } /** * 将任意进制数字转为10进制数字 */ private function _conv2hex10($int,$in_hex){ $int = strtoupper(trim($int)); if($in_hex==10){ return $int; }elseif( $in_hex== 2 && function_exists('bindec')){ return bindec($int); } elseif($in_hex== 16 && function_exists('hexdec')){ return hexdec($int); } elseif($in_hex== 8 && function_exists('octdec')){ return octdec($int); } $array = array(); $result = 0; for($i=0;$i<strlen($int);$i++){ array_unshift( $array, substr($int,$i,1)); //插入到数组头部(既倒序) } foreach($array as $k => $v){ $hex10_value = array_search($v,self::$element); if($hex10_value==-1){ return false; } $result += intval( pow($in_hex,$k) * $hex10_value ); } return $result; } /** * 把10进制数换成任意进制数 */ private function _conv_hex($hex_10,$out_hex){ $hex_10 = intval($hex_10); if($out_hex==10){ return $hex_10; }else if( $out_hex==2 && function_exists('decbin')){ return decbin($hex_10); } elseif ( $out_hex ==16 && function_exists('dechex')){ return dechex($hex_10); } elseif ( $out_hex ==8 && function_exists('decoct')){ return decoct($hex_10); } $array = array(); $result = ""; //利用10进制数除任意进制数 倒取余数法转换。 do { array_unshift( $array, $hex_10 % $out_hex); //余数插入到数组数组第1个位置。 $hex_10 = $hex_10 / $out_hex ; //除法 } while ($hex_10>1); foreach($array as $k){ $result .= self::$element[$k]; } return $result; } } ?>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

Dreamweaver Mac版
시각적 웹 개발 도구
