raw code

PHP Infusion Extension

Robert Eisele

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.


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


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

bool isbit(int $mask, int $n);


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

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


Toggle/invert a bit-flag in the number.

int invbit(int $mask, int $n);


Rotate a bit-mast.

int rotbit(int $mask, int $n);


Count the number of bits set in the number.

int numbit(int $mask);


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

int msbit(int $mask);


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);


Set a number in a circumscribed range of x and y

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

Logical Functions


Check if the number is between the two other arguments.

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

Mathematical Functions


Limits a number to a specified upper max value.

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


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

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


Returns the sign of a number.

int sgn(double $num);


Gives the greatest proper power in the number.

int gpp(int $num);


Calculates the significant figures of a number.

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


Calculate the checksum of a number.

int checksum(int $num);


Round to the next multiple of a base.

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


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

int xround(int $num);

Array Functions


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

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

String Functions


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

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


Offsets a string by a given number.

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


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

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


Check if the specified character is upper-case.

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


Check if the specified character is lower-case.

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


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

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


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]);


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]);


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

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

Misc Functions


Chops a number as big as possible into parts.

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


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


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


Check if there is a reference on a variable.

bool is_ref($var);


Installing from sources

  1. Download infusion source package
  2. Unpack infusion source package
  3. Go to infusion folder and type "phpize && ./configure && make && make install"
  4. Make sure you have extension=infusion.so in your php.ini

Compiling Infusion into PHP

  1. Download infusion source package
  2. Unpack infusion source package to $PHP_SOURCE_DIR/ext/infusion
  3. In php source root directory run commands: "rm configure && ./buildconf --force"
  4. Configure PHP with command "./configure --with-infusion"
  5. Run make && make install