PHP Infusion Extension

PHP Infusion combines a set of functions missed in the PHP core. While I worked on web projects and missed some functionality, I added the missed functions to this extension. I've recently published an extension for MySQL with many of the following functions, too.

Overview

This extension is a small set of (long?) missed functions in PHP. I generalized this extension for a public purpose. All other functions/changes will be published on a different way.

Binary Functions

isbit

Check if a bit-flag is set in the number.

setbit

Set a bit-flag in the number to 1 or another boolean.

int setbit(int \$mask, int \$n[, bool \$init=1]);

invbit

Toggle/invert a bit-flag in the number.

rotbit

Rotate a bit-mast.

numbit

Count the number of bits set in the number.

msbit

Get the most significant bit of a number. This equals to the log base 2 of the number.

getint

Get a range of bits out of a number circumscribed by x and y. For more information see Small numbers in big numbers.

int getint(int \$mask, int \$x, int \$y);

setint

Set a number in a circumscribed range of x and y

int setint(int \$mask, int \$x, int \$y, int \$num);

Logical Functions

between

Check if the number is between the two other arguments.

bool between(int \$num, int \$a, int \$b);

Mathematical Functions

limit

Limits a number to a specified upper max value.

int limit(int \$num, int \$max);

bound

Limits a number to a specified lower min- and a upper max value

int bound(int \$num, int \$min, int \$max);

sgn

Returns the sign of a number.

int sgn(double \$num);

gpp

Gives the greatest proper power in the number.

int gpp(int \$num);

sigfig

Calculates the significant figures of a number.

double sigfig(double \$num, int \$figs);

checksum

Calculate the checksum of a number.

int checksum(int \$num);

bround

Round to the next multiple of a base.

int bround(int \$num, int \$base);

xround

Round to the next power of 10. This breaks down 10log(n) / log(10)

int xround(int \$num);

Array Functions

kimplode

Joins array keys placing glue string between items and return one string.

string kimplode([string \$glue,] array \$pieces);

String Functions

strcut

Cuts a string if it's longer then a max value.

string strcut(string \$str, int \$num);

stroff

Offsets a string by a given number.

string stroff(string \$str, int \$off);

truncate

Cuts a string if it's longer then a max value and appends a given string.

string truncate(string \$str, int \$num[, string \$x='...']);

isuc

Check if the specified character is upper-case.

bool isuc(string \$str[, int \$pos=0]);

islc

Check if the specified character is lower-case.

bool islc(string \$str[, int \$pos=0]);

xsprintf

A more intelligent sprintf where you can specify the search token and a callback function.

string xsprintf(string \$format, callback \$callback, char \$delim);

strcal

String calibration to check, if the string is in a given format with a simple regexp format.

string strcal(string \$format, string \$str[, int \$len=-1]);

strical

String calibration without care about upper and lower case to check, if the string is in a given format with a simple regexp format.

string strical(string \$format, string \$str[, int \$len=-1]);

strmap

Brings a simple template parser to PHP. The idea comes from the C# printf() functionalitys.

string strmap(string \$str, array \$replace);

Misc Functions

number_chop

Chops a number as big as possible into parts.

array number_chop(int \$num, int \$parts);

time_chop

Chops a time value and returns as format string or as array.

[string|array] time_chop(int \$time[, mixed \$format=2, bool \$is_array=0]);

The format is a mixed type and can be defined as integer to define a number of entities or as string to define the units you want to get. The types for the selective mode are defined as:

y - years
n - months
w - weeks
d - days
h - hours
m - minutes
s - seconds

The time value switches to the delta of the current time and the passed value if the value is too big and looks by a unix timestamp.

Variable Functions

typeof

Returns the type of a number as integer.

int typeof(\$var);

The types are the same as the internal type mappings of the Zend-Engine. Here the mapping from zend.h header file:

#define IS_NULL		0
#define IS_LONG		1
#define IS_DOUBLE	2
#define IS_STRING	3
#define IS_ARRAY	4
#define IS_OBJECT	5
#define IS_BOOL		6
#define IS_RESOURCE	7

is_ref

Check if there is a reference on a variable.

bool is_ref(\$var);