PHP Classes

Bin String: Manipulate text with mbstring extension functions

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Info   View files Example   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2015-08-13 (3 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 166 All time: 7,774 This week: 681Up
Version License PHP version Categories  
bin-string 0.2GNU Lesser Genera...5.0PHP 5, Text processing
Description Author  

This class can manipulate text with mbstring extension functions.

It can check if the mbstring extension is enabled and provides wrapper functions that use that extension functions if possible or fallback to the respective non-multibyte text function versions.

When PHP is configured to overload the mbstring functions, it can still use the original versions of those functions.

Currently it provides wrapper functions for the functions: mail, strlen, strpos, strrpos, substr, strtolower, strtoupper, substr_count, ereg, eregi, ereg_replace, eregi_replace, and split.

Innovation Award  
PHP Programming Innovation award nominee
July 2014
Number 6

Prize: One downloadable e-book of choice by O'Reilly
The PHP mbstring extension provides functions to manipulate text strings that use character set encodings that may require more than one byte per character.

This allows PHP to manipulate strings with characters in practically any alphabet. However the mbstring extension functions are slower than similar functions for single byte character strings.

This class is a wrapper to use string manipulation functions that may use mbstring functions or not, depending on whether this extension is available in the current PHP environment, and also whether the application is using a single byte or multi-byte character set encodings for its texts.

Manuel Lemos
Picture of Asbjorn Grandt
Name: Asbjorn Grandt <contact>
Classes: 10 packages by
Country: Denmark Denmark
Age: 44
All time rank: 2151 in Denmark Denmark
Week rank: 57 Up1 in Denmark Denmark Equal
Innovation award
Innovation award
Nominee: 4x


Binary Safe String functions

If you use PHP's mbstring.func_overload, or the server you are running on has it enabled, you are in trouble. Especially if you are relying on being able to parse binary data and protocols.


To the question "Should I use multi-byte overloading (mbstring.func_overload)?". user 'gphilip' said it well on this StackOverflow post:

> My answer is: definitely not! > > The problem is that there is no easy way to "reset" > str* functions once they are overloaded. > > For some time this can work well with your project, > but almost surely you will run into an external library > that uses string functions to, for example, implement a > binary protocol, and they will fail. They will fail and > you will spend hours trying to find out why they are > failing.


This class is a wrapper for string functions, in cases where the mbstring.func_overload tripe have been enabled. Be warned, use this class ONLY if you have to, as it will affect performance a bit. For some functions, a lot, though that is due to problems in mb_string, not this class. Function calls in PHP are fairly expensive on their own, and if func_overload is enabled, it'll use mb_string functions exclusively in place of the built-in PHP string, to parse them as 'latin1', which is also expensive, cpu wise.

Why the potential performance impact?

PHP, like Java, have length aware strings, meaning the object header knows how long your string is. They are binary safe, and not null (0x00) terminated.

mb_string functions ignore that, and parse the entirety of the string, to figure out what is what. strlen(string) simply tells you how many bytes are in it, mb_strlen will parse it, to find multi byte characters, and tell you how many characters there are. That is great for handling multi-byte encoded strings correctly, such as UTF-8, it sucks for binary data handling, as multi-byte sequences are bound to occur by random chance, in any large enough binary data set.

  Files folder image Files  
File Role Description
Accessible without login Plain text file BinString.Example1.php Example Example and test file
Accessible without login Plain text file BinString.Example2.php Example Example of using the new "Static" version of BinString.
Plain text file BinString.php Class Main class
Plain text file BinStringStatic.php Class Same as BinString.php, but using static functions, rather than having to be instantiated.
Accessible without login Plain text file composer.json Data Composer file for this package.
Accessible without login Plain text file readme.markdown Doc. Readme

 Version Control Reuses Unique User Downloads Download Rankings  
 100%3Total:166All time:7,774
 This week:0This week:681Up