PHP 5.3 Fluent String Interface

1.1 2013-04-07 20:19 UTC


String Logo

PHP 5.3 String manipulation Class

Build Status

I was inspired to write this after viewing SimpleString by klaussilveira

String is a library that turns your strings in PHP into objects. This allows you to have a more object oriented approach to executing PHP's built in string functions using a fluent interface. The objective of this library was to create a readable way to execute all of your string functions and have the manipulated string returned at the end, without the need for nesting functions.


Steller Logo designed by: Checkout his GitHub

Installing With Composer


require: "williamknauss/string": "1.0"

to your composer.json and then execute:

composer update

Installing Manually

Download the latest build and include "string.php"


Basic Idea

The classic PHP way of trimming a string is:

$string = " Hello World";
$string = trim($string);
//Hello World

Using the String class we can do the same:

$string = " Hello World";
$string = new williamknauss\String($string);
$string = $string->trim();
//Hello World

Notice we didn't pass the original string to the trim method. This is because the string class keeps all of the evolutions that occur to your starting string and automaticlly sends the last evolution to the requested method. We will review later how to retrieve all of the evolutions, so you can compare them.

Lets expand on the trim, and add a string replacement.

In classic PHP it would like something like this:

$string = " Hello World";
$string = trim($string);
$string = str_replace("Hello","Goodbye",$string);
//Goodbye World

Using the String class it gets a bit simplier.

$string = " Hello World";
$string = new williamknauss\String($string);
//Goodbye World

Lets take this example one step further and use substr to retrieve the first 4 characters of our string.

The classic PHP would look something like this:

$string = " Hello World";
$string = trim($string);
$string = str_replace("Hello","Goodbye",$string);
$string = substr($string,0,4);

Again using our String library it looks cleaner, and with less typing:

$string = " Hello World";

String Functions that don't return strings

You will notice you can directly echo the variable that you have specified as the String object.


$string = new williamknauss\String(" Hello World");
echo $string->trim();
//Hello World

This is because of PHP's magic method __toString. This only works if the last evolution to your starting string is a string.

Take this for example.

$string = new williamknauss\String("Column1,Column2,Column3");
echo $string->explode(",");

When the last evolution to occur to your string is not a string, and cannot be casted into a string the library returns with an empty string to prevent errors from occuring. To get around this you can tell the string library to return a specific data type. There are two ways of doing this. You can either call direct_return() which will return the last evolution to your starting string, or you can call return_{datatype}*. If you call return_{datatype} the string class will verify that the last evolution datatype is matching your requested datatype. If it doesn't an exception will be thrown.

*At this point there are two datatype return methods.

  • return_string
  • return_array

Exposed Methods

All the string functions listed on PHP's manual page excepted the following are exposed as method calls in the String class.

  • convert_cyr_string
  • fprintf
  • hebrev
  • hebrevc
  • hex2bin
  • money_format
  • nl_langinfo
  • strtok
  • vfprintf
  • vprintf
  • vsprintf

Natural Methods

Natural methods are class methods that were either directly created for this class, or native PHP string functions that had parameters in a certain order. Here is a list of all the natural methods that occur in this class.

  • set_string
  • evolution_counter
  • _call_last_evolution
  • _agitate_arguments
  • return_string
  • return_array
  • chr
  • md5_file
  • sha1_file
  • implode
  • join
  • explode
  • get_html_translation_table
  • localeconv
  • number_format
  • hash
  • echos
  • prints
  • str_replace
  • str_ireplace
  • direct_return
  • undo
  • redo
  • concatenate
  • append
  • prepend
  • hashit
  • shorten

Native PHP Functions

Native PHP functions are class methods that did not require any additional coding and can be used just like their native PHP function counterpart. Here is a list of all the native PHP functions that are supported in this class.

  • addcslashes
  • addslashes
  • bin2hex
  • chop
  • chunk_split
  • convert_uudecode
  • convert_uuencode
  • crypt
  • html_entity_decode
  • htmlentities
  • htmlspecialchars_decode
  • htmlspecialchars
  • lcfirst
  • ltrim
  • md5
  • nl2br
  • quoted_printable_decode
  • quoted_printable_encode
  • rtrim
  • sha1
  • soundex
  • sprintf
  • str_pad
  • str_repeat
  • str_rot13
  • str_shuffle
  • strip_tags
  • stripcslashes
  • stripslashes
  • strrev
  • strtolower
  • strtoupper
  • trim
  • ucfirst
  • ucwords
  • wordwrap
  • crc32
  • metaphone
  • count_chars
  • levenshtein
  • localeconv
  • number_format
  • ord
  • hash
  • echo
  • print
  • printf
  • quotemeta
  • setlocale
  • similar_text
  • sscanf
  • str_getcsv
  • str_split
  • str_word_count
  • strcasecmp
  • strstr
  • strchr
  • strcmp
  • strcoll
  • strcspn
  • stripos
  • stristr
  • strlen
  • strnatcasecmp
  • strnatcmp
  • strncasecmp
  • strncmp
  • strpbrk
  • strpos
  • strrchr
  • strripos
  • strrpos
  • strspn
  • strtr
  • substr_compare
  • substr_count
  • substr_replace
  • substr