what im trying to do:
1. uncompress compressed file.
2.insert the uncompressed file bytes into array.
3.convert each byte to chr.
the code:
<?php
$list = file_get_contents('http://www.getlist.xp3.biz/list3'); //get the contents of the compressed file
$list = gzuncompress($list); //Uncompress the file
$bytes = unpack('c*', $list); //unpack the bytes of list into array
$string = implode('', array_map('chr', $bytes)); //convert each byte to chr by implode and array_map functions
echo $string; //print results
?>
this code is almost works.
the results:
http://getlist.xp3.biz/getlist.php
lets take this line for example:
B~12ee397313ba2dd4f27fc1430744e615e4f83a44f9b206fb78fdf9b45dd9dc74fel profanador88's Room ar�7t��Bk
in the end of the line there are some wired chars : ar�7t��Bk
this chars are not text byte, someone(other developer) is converted the bytes and his php file returns the line with more information:
{"ver":"26","id":"12ee397313ba2dd4f27fc1430744e615e4f83a44f9b206fb78fdf9b45dd9dc74f","name":"profanador88's Room","players":"1","max_players":"10","password":false,"country":"ar","latitude":"41.0186004639","longitude":"28.9647006989","distance":1168.7633}
i have the id ,roomname and country code(and the werid chars(=the other info)).
in his line: version,id, name ,players number,max player number,if isset password(boolean),contry code, latitude,longitude and distance.
exactly 7 more.
maybe ar�7t��Bk contains the other information?
i think that the other info is there (100%), i just dont know how to convert the bytes(of the werid chars).
i tried to ask the other developer about this werid chars but there is no answer.
maybe someone know how to convert this binary file into normal string,
maybe this information can help(the note of the other developer):
the list is a compressed binary file
which you can obtain # /list3
it is compressed with zlib deflate
after decompression the format is as follows: (wait a bit must open the source file)
1 byte ; list format version ( currently = 1 )
4 bytes ; skip this, you don't care about them
then follows a list of RoomInfo structures until the end of the file
this is the Roominfo structure:
data.writeShort(ver); //Vrsion Short
data.writeUTF(netID); //ID String
data.writeUTF(name); //ROOMNAME string
data.writeByte(players); //PLAYERS
data.writeByte(maxPlayers); //MaxPlayers
data.writeBoolean(password); //If isset password boolean
data.writeUTF(countryCode); //country code
data.writeFloat(latitude); //Float latitude
data.writeFloat(longitude); //Float longitude
that's all
im using chr function to convert the bytes into big string, hi says that the byte are not only strings, there are bytes that represent:Float,Boolean,Integer,Short.
chr is to bytes that represent string only(and i used it : $string = implode('', array_map('chr', $bytes)); ), there im worng maybe i need to read each byte by foreach without using this shortcut.
my question is:
Does anyon know how to fix the code and make it work(make the php print the other info that missing(the info is there , but in bytes(not of string)))?
UPDATE:
i think that i found the class that converts byte to short/boolean/float/integer ,
now i need that someone explain to me how to use this class (require('class.php')...code here), or someone will try to fix my code with this class(im trying to fix it alone but i still dont know how to use this lib or when i need to use the lib).
the class:
class ByteArray{
private $BytesString;
function ByteArray($bytes = "") {
$this->BytesString = $bytes;
}
function writeBoolean($value = 1) {
$this->BytesString .= $this->writeByte($value, False);
}
function writeByte($value, $noReturning=True) {
if ($noReturning) $this->BytesString .= pack("C", $value);
else return pack("C", $value);
}
function writeBytes($value) {
$this->BytesString .= $value;
}
function writeInt($value) {
$this->BytesString .= pack('N', $value);
}
function writeShort($value) {
$this->BytesString .= pack('n', $value);
}
function writeUTF($value) {
$valueSize = strlen($value);
$this->writeShort($valueSize);
$this->writeUTFBytes($value);
}
function writeUTFBytes($value) {
$this->BytesString .= $value;
}
function length() {
return strlen($this->BytesString);
}
function toString() {
return $this->BytesString;
}
function toPack() {
$value = pack('N', strlen($this->BytesString)+4);
return $value.$this->BytesString;
}
function getSize() {
$value = unpack('N', substr($this->BytesString, 0, 4));
return $value[1];
}
function readBy($Pos) {
$this->BytesString = substr($this->BytesString, $Pos);
return $this->BytesString;
}
function loc($byte) {
$loc = substr($this->BytesString, 0, $byte);
$this->BytesString = substr($this->BytesString, $byte);
return unpack('C', $loc);
}
function readInt() {
$size = unpack('N', substr($this->BytesString, 0, 4)); $size = $size[1];
$this->BytesString = substr($this->BytesString, 4);
return $size;
}
function readUTF() {
$size = unpack('n', substr($this->BytesString, 0, 2)); $size = $size[1];
$string = substr($this->BytesString, 2, $size);
$this->BytesString = substr($this->BytesString, $size + 2);
return $string;
}
function readShort() {
$size = unpack('n', substr($this->BytesString, 0, 2)); $size = $size[1];
$this->BytesString = substr($this->BytesString, 2);
return $size;
}
function readBoolean() {
$loc = unpack('C', substr($this->BytesString, 0, 1)); $loc = $loc[1];
$this->BytesString = substr($this->BytesString, 1);
if ($loc == 1) return True;
else return False;
}
function readByte() {
$byte = unpack('C', substr($this->BytesString, 0, 1)); $byte = $byte[1];
$this->BytesString = substr($this->BytesString, 1);
return $byte;
}
}
UPDATE2:
version 2 of the class, if the first not working
<?php
class ByteArray {
protected $bigEndian = TRUE;
protected $byteArray;
protected $capacity;
protected $limit;
protected $mark;
public $position;
public function __construct($byteArray = '') {
$this->byteArray = $byteArray;
$this->position = 0;
$this->mark = - 1;
$this->init ();
}
private function init() {
$this->capacity = strlen ( $this->byteArray );
$this->limit = $this->capacity;
}
public function _array() {
return $this->byteArray;
}
public function clear() {
$this->limit = $this->capacity;
$this->position = 0;
$this->mark = - 1;
}
private function get($length = null) {
if ($length === null) {
$length = $this->limit - $this->position;
} elseif ($length > $this->bytesAvailable ()) {
throw new Exception ( 'bytesAvailable' );
}
$data = substr ( $this->byteArray, $this->position, $length );
$this->position += $length;
return $data;
}
private function set($bytes) {
$p1 = substr ( $this->byteArray, 0, $this->position );
$p2 = substr ( $this->byteArray, $this->position );
$len = strlen ( $bytes );
if ($len < strlen ( $p2 )) {
$p2 = substr ( $p2, $len );
} else {
$p2 = '';
}
$p1 .= $bytes . $p2;
$this->byteArray = $p1;
$this->position += $len;
$this->init ();
}
public function readBytes($length = -1, $offset = -1) {
$limit = $this->limit;
if ($offset == - 1) {
$offset = $this->position;
}
if ($length == - 1) {
$length = $limit - $offset;
}
if ($length > $limit - $offset) {
return null;
}
return substr ( $this->byteArray, $offset, $length );
}
public function writeBytes($bytes, $offset = 0, $length = 0) {
$len = strlen ( $bytes );
if ($len < 1) {
return;
}
if ($length < 1) {
$length = $len;
}
if ($offset < 1) {
$offset = 0;
}
if ($offset + $length > $len) {
return;
}
$p1 = substr ( $bytes, $offset, $length );
$this->set ( $p1 );
}
public function readBoolean() {
return $this->readByte () != 0;
}
public function writeBoolean($value) {
$this->writeByte ( $value != 0 );
}
public function readByte() {
return ord ( $this->get ( 1 ) );
}
public function readUnsignedByte() {
$data = unpack ( 'C', $this->get ( 1 ) );
return $data [1];
}
public function writeByte($value) {
$data = pack ( 'c', $value );
$this->set ( $data );
}
public function readShort() {
$data = unpack ( $this->bigEndian ? 'n' : 'v', $this->get ( 2 ) );
return $data [1];
}
public function writeShort($value) {
$data = pack ( $this->bigEndian ? 'n' : 'v', $value );
$this->set ( $data );
}
public function readInt() {
$data = unpack ( $this->bigEndian ? 'N' : 'V', $this->get ( 4 ) );
return $data [1];
}
public function writeInt($value) {
$data = pack ( $this->bigEndian ? 'N' : 'V', $value );
$this->set ( $data );
}
public function readFloat() {
$data = unpack ( 'f', $this->get ( 4 ) );
return $data [1];
}
public function writeFloat($value) {
$data = pack ( 'f', $value );
$this->set ( $data );
}
public function readDouble() {
$data = unpack ( 'd', $this->get ( 8 ) );
return $data [1];
}
public function writeDouble($value) {
$data = pack ( 'd', $value );
$this->set ( $data );
}
public function readString() {
$length = $this->readShort ();
$value = $this->get ( $length );
return $value;
}
public function writeString($value) {
$len = strlen ( $value );
$this->writeShort ( $len );
$this->writeStringBytes ( $value );
}
public function writeStringBytes($value) {
$len = strlen ( $value );
$data = pack ( 'a' . $len, $value );
$this->set ( $data );
}
public function readStringBytes($length) {
return $this->get ( $length );
}
public function bytesAvailable() {
return $this->limit - $this->position;
}
public function length() {
return $this->limit;
}
public function __toString() {
return $this->byteArray;
}
public function compress($level = 5) {
$this->byteArray = gzcompress ( $this->byteArray, $level );
$this->init ();
}
public function uncompress($level = 5) {
$this->byteArray = gzuncompress ( $this->byteArray, $level );
$this->init ();
}
}
?>
Related
I am trying to build a multidimensional array in PHP using an array of string that I am cutting up so the string of 1:wlrb#yahoo.com:7:8.35 becomes
"id": "1",
"email_address": "wlrb#yahoo.com",
"domain": "yahoo.com",
"number_of_orders": "7",
"total_order_value": "£8.35"
In JavaScript I would create an object containing the above values and put it into an array but what is the equivalent in PHP?
So far I have the below code which gives me
Array ( [0] => stdClass Object ( [id] => 1 [email] => wlrb#yahoo.com
[domain] => yahoo.com [number_of_orders] => 7 [total_order_value] =>
8.35 )
<?php
$data = file_get_contents('orderdata');
/* echo $data; */
$lines = explode("\n", $data);
array_splice($lines, 0, 8);
/* var_dump($lines); */
array_splice($lines, -3);
/* var_dump($lines); */
/* removes all NULL, FALSE and Empty Strings but leaves 0 (zero) values */
$lines = array_values(array_filter($lines, 'strlen'));
function arraySort($lines ,$i) {
$rep = new stdClass();
$rep->id = strId($lines, $i);
$rep->email = strEmail($lines, $i);
$rep->domain = strDomain($lines, $i);
$rep->number_of_orders = orderNo($lines, $i);
$rep->total_order_value = orderValue($lines, $i);
/* var_dump($rep); */
return $rep;
}
function strDomain($lines, $i) {
if($lines[$i] == null){
return "";
}
else {
$str = $lines[$i];
$splt = explode(':', $str);
$domain = explode('#', $splt[1]);
return $domain[1];
}
}
function strId($lines, $i) {
if($lines[$i] == null){
return "";
}
else {
$str = $lines[$i];
$splt = explode(':', $str);
return $splt[0];
}
}
function strEmail($lines, $i) {
if($lines[$i] == null){
return "";
}
else {
$str = $lines[$i];
$splt = explode(':', $str);
return $splt[1];
}
}
function orderNo($lines, $i) {
if($lines[$i] == null){
return "";
}
else {
$str = $lines[$i];
$splt = explode(':', $str);
return $splt[2];
}
}
function orderValue($lines, $i) {
if($lines[$i] == null){
return "";
}
else {
$str = $lines[$i];
$splt = explode(':', $str);
return '£' + $splt[3];
}
}
$reports = array();
$reps = array();
for($i = 0, $length = count($lines); $i < $length; ++$i) {
$reps = arraySort($lines, $i);
array_push($reports, $reps);
}
?>
but when I try to search the array with
$filteredArray =
array_filter($reports, function($element) use($search){
return isset($element['domain']) && $element['domain'] == $search;
});
I get the following error
Fatal error: Uncaught Error: Cannot use object of type stdClass as
array in phpData.php:110 Stack trace: #0
[internal function]: {closure}(Object(stdClass)) #1
phpData.php(111): array_filter(Array,
Object(Closure)) #2 {main} thrown in
phpData.php on line 110
Is this because of the use of $rep = new stdClass();in my arraySort function? If so what should I be using?
The easiest and shortest solution would be :
$value = "1:wlrb#yahoo.com:7:8.35";
$keys = array('id', 'email_address', 'number_of_orders', 'total_order_value');
$fused = array_combine($keys, explode(':', $value));
$fused['domain'] = strDomain($fused['email_address']); //using your "strDomain()" function
It will give you the array you want, except you won't have the £ sign in your order value.
you can use object obtention method like this:
$filteredArray =
array_filter($reports, function($element) use($search){
return isset($element->domain) && $element->domain == $search;
});
I have an existing php script written which checks if an ip has been written to a .dat, and redirects the user to in this case google if the ip is in the file. If not, it lets them continue on with signing up to my site.
The code below checks if the control is switched on. I placed it in the index.php
if($One_time_access==1)
{
$fp = fopen("blacklist.dat", "a");
fputs($fp, "\r\n$ip\r\n");
fclose($fp);
}
Then the code below does all the work to figure out the ip, what to ban, etc
<?php
class IpList {
private $iplist = array();
private $ipfile = "";
public function __construct( $list ) {
$contents = array();
$this->ipfile = $list;
$lines = $this->read( $list );
foreach( $lines as $line ) {
$line = trim( $line );
# remove comment and blank lines
if ( empty($line ) ) {
continue;
}
if ( $line[0] == '#' ) {
continue;
}
# remove on line comments
$temp = explode( "#", $line );
$line = trim( $temp[0] );
# create content array
$contents[] = $this->normal($line);
}
$this->iplist = $contents;
}
public function __destruct() {
}
public function __toString() {
return implode(' ',$this->iplist);
}
public function is_inlist( $ip ) {
$retval = false;
foreach( $this->iplist as $ipf ) {
$retval = $this->ip_in_range( $ip, $ipf );
if ($retval === true ) {
$this->range = $ipf;
break;
}
}
return $retval;
}
/*
** public function that returns the ip list array
*/
public function iplist() {
return $this->iplist;
}
/*
*/
public function message() {
return $this->range;
}
public function append( $ip, $comment ) {
return file_put_contents( $this->ipfile, $ip, $comment );
}
public function listname() {
return $this->ipfile;
}
/*
** private function that reads the file into array
*/
private function read( $fname ) {
try {
$file = file( $fname, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
}
catch( Exception $e ) {
throw new Exception( $fname.': '.$e->getmessage() . '\n');
}
return $file;
}
private function ip_in_range( $ip, $range ) {
// return ip_in_range( $ip, $range );
if ( strpos($range, '/') !== false ) {
// IP/NETMASK format
list( $range, $netmask ) = explode( '/', $range, 2 );
if ( strpos( $netmask, '.' ) !== false ) {
// 255.255.255.0 format w/ wildcards
$netmask = str_replace('*', '0', $netmask );
$dnetmask = ip2long( $netmask );
return ((ip2long( $ip ) & $dnetmask) == (ip2long($range) & $dnetmask ));
}
else {
// IP/CIDR format
// insure $range format 0.0.0.0
$r = explode( '.', $range );
while( count( $r ) < 4 ) {
$r[] = '0';
}
for($i = 0; $i < 4; $i++) {
$r[$i] = empty($r[$i]) ? '0': $r[$i];
}
$range = implode( '.', $r );
// build netmask
$dnetmask = ~(pow( 2, ( 32 - $netmask)) - 1);
return ((ip2long($ip) & $dnetmask)==(ip2long($range) & $dnetmask));
}
}
else {
if ( strpos( $range, '*' ) !== false ) {
// 255.255.*.* format
$low = str_replace( '*', '0', $range );
$high = str_replace( '*', '255', $range );
$range = $low.'-'.$high;
}
if ( strpos( $range, '-') !== false ) {
// 128.255.255.0-128.255.255.255 format
list( $low, $high ) = explode( '-', $range, 2 );
$dlow = $this->toLongs( $low );
$dhigh = $this->toLongs( $high );
$dip = $this->toLongs( $ip );
return (($this->compare($dip,$dlow) != -1) && ($this->compare($dip,$dhigh) != 1));
}
}
return ( $ip == $range );
}
private function normal( $range ) {
if ( strpbrk( "*-/", $range ) === False ) {
$range .= "/32";
}
return str_replace( ' ', '', $range );
}
private function toLongs( $ip ) {
# $Ip = $this->expand();
# $Parts = explode(':', $Ip);
$Parts = explode('.', $ip );
$Ip = array('', '');
# for ($i = 0; $i < 4; $i++) {
for ($i = 0; $i < 2; $i++){
$Ip[0] .= str_pad(base_convert($Parts[$i], 16, 2), 16, 0, STR_PAD_LEFT);
}
# for ($i = 4; $i < 8; $i++) {
for ($i = 2; $i < 4; $i++) {
$Ip[1] .= str_pad(base_convert($Parts[$i], 16, 2), 16, 0, STR_PAD_LEFT);
}
return array(base_convert($Ip[0], 2, 10), base_convert($Ip[1], 2, 10));
}
private function compare( $ipdec1, $ipdec2 ) {
if( $ipdec1[0] < $ipdec2[0] ) {
return -1;
}
elseif ( $ipdec1[0] > $ipdec2[0] ) {
return 1;
}
elseif ( $ipdec1[1] < $ipdec2[1] ) {
return -1;
}
elseif ( $ipdec1[1] > $ipdec2[1] ) {
return 1;
}
return 0;
}
}
class IpBlockList {
private $statusid = array( 'negative' => -1, 'neutral' => 0, 'positive' => 1 );
private $whitelist = array();
private $blacklist = array();
private $message = NULL;
private $status = NULL;
public function __construct(
$whitelistfile = 'assets/includes/whitelist.dat',
$blacklistfile = 'assets/includes/blacklist.dat' ) {
$this->whitelistfile = $whitelistfile;
$this->blacklistfile = $blacklistfile;
$this->whitelist = new IpList( $whitelistfile );
$this->blacklist = new IpList( $blacklistfile );
}
public function __destruct() {
}
public function ipPass( $ip ) {
$retval = False;
if ( !filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
throw new Exception( 'Requires valid IPv4 address' );
}
if ( $this->whitelist->is_inlist( $ip ) ) {
// Ip is white listed, so it passes
$retval = True;
$this->message = $ip . " is whitelisted by ".$this->whitelist->message().".";
$this->status = $this->statusid['positive'];
}
else if ( $this->blacklist->is_inlist( $ip ) ) {
$retval = False;
$this->message = $ip . " is blacklisted by ".$this->blacklist->message().".";
$this->status = $this->statusid['negative'];
}
else {
$retval = True;
$this->message = $ip . " is unlisted.";
$this->status = $this->statusid['neutral'];
}
return $retval;
}
public function message() {
return $this->message;
}
public function status() {
return $this->status;
}
public function append( $type, $ip, $comment = "" ) {
if ($type == 'WHITELIST' ) {
$retval = $this->whitelistfile->append( $ip, $comment );
}
elseif( $type == 'BLACKLIST' ) {
$retval = $this->blacklistfile->append( $ip, $comment );
}
else {
$retval = false;
}
}
public function filename( $type, $ip, $comment = "" ) {
if ($type == 'WHITELIST' ) {
$retval = $this->whitelistfile->filename( $ip, $comment );
}
elseif( $type == 'BLACKLIST' ) {
$retval = $this->blacklistfile->filename( $ip, $comment );
}
else {
$retval = false;
}
}
}
$ips = array( $_SERVER['REMOTE_ADDR'],
);
$checklist = new IpBlockList( );
foreach ($ips as $ip ) {
$result = $checklist->ipPass( $ip );
if ( $result ) {
// Continue with page
}
else {
header("Location: https://www.google.co.uk/");
die();
}
}
?>
I want to know:
1. can i write the ips to a text file instead of .dat?
2. is there an easier way to do this?/shorter script?
All help will be appreciated :)
I'm trying to solve a somewhat simple task but can't wrap my head around doing it without a lot of loops and messy code.
I want all to print all combinations of the array below:
$product = array(
array
(
'1X'
),
array
(
'X2'
)
);
producing the following result:
//ROW 1
1
X
//ROW 2
X
2
//ROW 3
1
2
//ROW 4
X
X
this work:
$product = array(
array
(
'1WQ'
),
array
(
'3'
),
array
(
'X'
)
);
//
class Combine{
private $product = array();
private $result = array();
private $format = array();
public function __construct($p=array()){
$this->product = $p;
}
public function process(){
foreach($this->product as $k=>$v){
$this->format[] = str_split($v[0]);
}
$this->result = $this->build();
return $this;
}
public function build()
{
if (!$this->format) {
return array(array());
}
$sub = array_shift($this->format);
$c = $this->build($this->format);
$res = array();
foreach ($sub as $v) {
foreach ($c as $p) {
array_unshift($p, $v);
$res[] = $p;
}
}
return $res;
}
public function response(){
return $this->result;
}
}
//
$combine = new Combine($product);
$resp = $combine->process()->response();
var_dump($resp);
function helperFunction($array, $index, $workArray)
{
$tempArray = array();
$tmpStr = $array[$index][0];
// loop over the current array characters
for( $i = 0; $i < strlen($tmpStr); $i++ )
{
$char = substr($tmpStr, $i, 1);
// first time - add characters to the work array
if (count($workArray) == 0)
{
$tempArray[] = $char;
}
// later round - add characters to existing items
else
{
foreach ($workArray as $workItem)
{
$tempArray[] = $workItem . $char;
}
}
}
// last round
if (count($array) == $index + 1)
{
return $tempArray;
}
// recursion round
else
{
return helperFunction($array, $index + 1, $tempArray);
}
}
$result = helperFunction($product, 0, array());
I have a folder that contains subfolders of years (e.g. 2011, 2012 etc.). Each year's folder contains month folders (e.g 02, 09 etc).
How do I calculate the total size of the month folders and list them along with the year and month folder names?
Example:
Directory Name - Size
06/2008 - 52KB
10/2010 - 151MB
27/2012 - 852MB
12/01/2013 - 5GB
Cheers.
You can try
echo "<pre>";
$depth = 1;
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
$r = array();
foreach ( $ritit as $splFileInfo ) {
if ($ritit->getDepth() === $depth && $splFileInfo->isDir()) {
printf("%s - %s \n", $splFileInfo, getSize($splFileInfo));
}
}
function getSize($dir, $precision = 2) {
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS));
$bytes = 0;
foreach ( $ritit as $v ) {
$bytes += $v->getSize();
}
$units = array('B','KB','MB','GB','TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
You can also try this approach using FilterIterator
$depth = 1;
$it = new RecursiveDirectoryIterator("./", RecursiveDirectoryIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST);
$it = new FileDepthFilterIterator($it, $depth, FileDepthFilterIterator::ONLY_DIR);
foreach ( $it as $splFileInfo ) {
printf("%s\n", new RecusiveSizeInfo($splFileInfo));
}
Classes used
class FileDepthFilterIterator extends FilterIterator {
private $it;
private $depth;
private $type;
const ONLY_DIR = 1;
const ONLY_FILE = 2;
const BOTH_DIR_FILE = 3;
function __construct(RecursiveIteratorIterator &$iterator, $depth, $type) {
$this->it = &$iterator;
$this->depth = $depth;
$this->type = $type;
parent::__construct($this->it);
}
function accept() {
if ($this->getDepth() != $this->depth) {
return false;
}
if ($this->type == self::ONLY_DIR && ! $this->getInnerIterator()->current()->isDir()) {
return false;
}
if ($this->type == self::ONLY_FILE && ! $this->getInnerIterator()->current()->isFile()) {
return false;
}
return true;
}
}
class RecusiveSizeInfo {
/**
*
* #var SplFileInfo
*/
private $info;
private $numFiles = 0;
private $numFolder = 0;
private $bytes = 0;
function __construct(SplFileInfo $info) {
$this->info = $info;
$this->parse();
}
public function getNumFiles() {
return $this->numFiles;
}
public function getNumFolder() {
return $this->numFolder;
}
public function getBytes() {
return $this->bytes;
}
public function __toString() {
return sprintf("%s\t%s\t%s", $this->info, $this->formatSize($this->getBytes()), json_encode(array("file" => $this->numFiles,"dir" => $this->numFolder)));
}
private function parse() {
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->info , RecursiveDirectoryIterator::SKIP_DOTS),RecursiveIteratorIterator::SELF_FIRST);
foreach ( $ritit as $v ) {
$v->isFile() and $this->numFiles ++;
$v->isDir() and $this->numFolder ++;
$this->bytes += $v->getSize();
}
}
private function formatSize($bytes) {
$units = array('B','KB','MB','GB','TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, 2) . ' ' . $units[$pow];
}
}
Look at this answer to work out the size of directories. To implement, you're going to need to traverse your directory structure recursively, or manually specify the directories you want:
<li>2012/01 <?php echo foldersize('/2012/01'); ?></li>
<li>2012/02 <?php echo foldersize('/2012/01'); ?></li>
It was hinted in a comment to an answer to this question that PHP can not reverse Unicode strings.
As for Unicode, it works in PHP
because most apps process it as
binary. Yes, PHP is 8-bit clean. Try
the equivalent of this in PHP: perl
-Mutf8 -e 'print scalar reverse("ほげほげ")' You will get garbage,
not "げほげほ". – jrockway
And unfortunately it is correct that PHPs unicode support atm is at best "lacking". This will hopefully change drastically with PHP6.
PHPs MultiByte functions does provide the basic functionality you need to deal with unicode, but it is inconsistent and does lack a lot of functions. One of these is a function to reverse a string.
I of course wanted to reverse this text for no other reason then to figure out if it was possible. And I made a function to accomplish this enormous complex task of reversing this Unicode text, so you can relax a bit longer until PHP6.
Test code:
$enc = 'UTF-8';
$text = "ほげほげ";
$defaultEnc = mb_internal_encoding();
echo "Showing results with encoding $defaultEnc.\n\n";
$revNormal = strrev($text);
$revInt = mb_strrev($text);
$revEnc = mb_strrev($text, $enc);
echo "Original text is: $text .\n";
echo "Normal strrev output: " . $revNormal . ".\n";
echo "mb_strrev without encoding output: $revInt.\n";
echo "mb_strrev with encoding $enc output: $revEnc.\n";
if (mb_internal_encoding($enc)) {
echo "\nSetting internal encoding to $enc from $defaultEnc.\n\n";
$revNormal = strrev($text);
$revInt = mb_strrev($text);
$revEnc = mb_strrev($text, $enc);
echo "Original text is: $text .\n";
echo "Normal strrev output: " . $revNormal . ".\n";
echo "mb_strrev without encoding output: $revInt.\n";
echo "mb_strrev with encoding $enc output: $revEnc.\n";
} else {
echo "\nCould not set internal encoding to $enc!\n";
}
here's another approach using regex:
function utf8_strrev($str){
preg_match_all('/./us', $str, $ar);
return implode(array_reverse($ar[0]));
}
Here's another way. This seems to work without having to specify an output encoding (tested with a couple of different mb_internal_encodings):
function mb_strrev($text)
{
return join('', array_reverse(
preg_split('~~u', $text, -1, PREG_SPLIT_NO_EMPTY)
));
}
Grapheme functions handle UTF-8 string more correctly than mbstring and PCRE functions/ Mbstring and PCRE may break characters. You can see the defference between them by executing the following code.
function str_to_array($string)
{
$length = grapheme_strlen($string);
$ret = [];
for ($i = 0; $i < $length; $i += 1) {
$ret[] = grapheme_substr($string, $i, 1);
}
return $ret;
}
function str_to_array2($string)
{
$length = mb_strlen($string, "UTF-8");
$ret = [];
for ($i = 0; $i < $length; $i += 1) {
$ret[] = mb_substr($string, $i, 1, "UTF-8");
}
return $ret;
}
function str_to_array3($string)
{
return preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
}
function utf8_strrev($string)
{
return implode(array_reverse(str_to_array($string)));
}
function utf8_strrev2($string)
{
return implode(array_reverse(str_to_array2($string)));
}
function utf8_strrev3($string)
{
return implode(array_reverse(str_to_array3($string)));
}
// http://www.php.net/manual/en/function.grapheme-strlen.php
$string = "a\xCC\x8A" // 'LATIN SMALL LETTER A WITH RING ABOVE' (U+00E5)
."o\xCC\x88"; // 'LATIN SMALL LETTER O WITH DIAERESIS' (U+00F6)
var_dump(array_map(function($elem) { return strtoupper(bin2hex($elem)); },
[
'should be' => "o\xCC\x88"."a\xCC\x8A",
'grapheme' => utf8_strrev($string),
'mbstring' => utf8_strrev2($string),
'pcre' => utf8_strrev3($string)
]));
The result is here.
array(4) {
["should be"]=>
string(12) "6FCC8861CC8A"
["grapheme"]=>
string(12) "6FCC8861CC8A"
["mbstring"]=>
string(12) "CC886FCC8A61"
["pcre"]=>
string(12) "CC886FCC8A61"
}
IntlBreakIterator can be used since PHP 5.5 (intl 3.0);
function utf8_strrev($str)
{
$it = IntlBreakIterator::createCodePointInstance();
$it->setText($str);
$ret = '';
$pos = 0;
$prev = 0;
foreach ($it as $pos) {
$ret = substr($str, $prev, $pos - $prev) . $ret;
$prev = $pos;
}
return $ret;
}
The answer
function mb_strrev($text, $encoding = null)
{
$funcParams = array($text);
if ($encoding !== null)
$funcParams[] = $encoding;
$length = call_user_func_array('mb_strlen', $funcParams);
$output = '';
$funcParams = array($text, $length, 1);
if ($encoding !== null)
$funcParams[] = $encoding;
while ($funcParams[1]--) {
$output .= call_user_func_array('mb_substr', $funcParams);
}
return $output;
}
Another method:
function mb_strrev($str, $enc = null) {
if(is_null($enc)) $enc = mb_internal_encoding();
$str = mb_convert_encoding($str, 'UTF-16BE', $enc);
return mb_convert_encoding(strrev($str), $enc, 'UTF-16LE');
}
It is easy utf8_strrev( $str ). See the relevant source code of my Library that I copied below:
function utf8_strrev( $str )
{
return implode( array_reverse( utf8_split( $str ) ) );
}
function utf8_split( $str , $split_length = 1 )
{
$str = ( string ) $str;
$ret = array( );
if( pcre_utf8_support( ) )
{
$str = utf8_clean( $str );
$ret = preg_split('/(?<!^)(?!$)/u', $str );
// \X is buggy in many recent versions of PHP
//preg_match_all( '/\X/u' , $str , $ret );
//$ret = $ret[0];
}
else
{
//Fallback
$len = strlen( $str );
for( $i = 0 ; $i < $len ; $i++ )
{
if( ( $str[$i] & "\x80" ) === "\x00" )
{
$ret[] = $str[$i];
}
else if( ( ( $str[$i] & "\xE0" ) === "\xC0" ) && ( isset( $str[$i+1] ) ) )
{
if( ( $str[$i+1] & "\xC0" ) === "\x80" )
{
$ret[] = $str[$i] . $str[$i+1];
$i++;
}
}
else if( ( ( $str[$i] & "\xF0" ) === "\xE0" ) && ( isset( $str[$i+2] ) ) )
{
if( ( ( $str[$i+1] & "\xC0" ) === "\x80" ) && ( ( $str[$i+2] & "\xC0" ) === "\x80" ) )
{
$ret[] = $str[$i] . $str[$i+1] . $str[$i+2];
$i = $i + 2;
}
}
else if( ( ( $str[$i] & "\xF8" ) === "\xF0" ) && ( isset( $str[$i+3] ) ) )
{
if( ( ( $str[$i+1] & "\xC0" ) === "\x80" ) && ( ( $str[$i+2] & "\xC0" ) === "\x80" ) && ( ( $str[$i+3] & "\xC0" ) === "\x80" ) )
{
$ret[] = $str[$i] . $str[$i+1] . $str[$i+2] . $str[$i+3];
$i = $i + 3;
}
}
}
}
if( $split_length > 1 )
{
$ret = array_chunk( $ret , $split_length );
$ret = array_map( 'implode' , $ret );
}
if( $ret[0] === '' )
{
return array( );
}
return $ret;
}
function utf8_clean( $str , $remove_bom = false )
{
$regx = '/([\x00-\x7F]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3})|./s';
$str = preg_replace( $regx , '$1' , $str );
if( $remove_bom )
{
$str = utf8_str_replace( utf8_bom( ) , '' , $str );
}
return $str;
}
function utf8_str_replace( $search , $replace , $subject , &$count = 0 )
{
return str_replace( $search , $replace , $subject , $count );
}
function utf8_bom( )
{
return "\xef\xbb\xbf";
}
function pcre_utf8_support( )
{
static $support;
if( !isset( $support ) )
{
$support = #preg_match( '//u', '' );
//Cached the response
}
return $support;
}