I was looking at Ruby and it has a very nice OO structure unlike PHP with C-like string functions. I was wondering if there is an extension which makes strings into objects so you could use them like this:
$str = "sometext";
echo "len:" . $str->length; //would print 'len: 8'
Take a look at this...
http://code.google.com/p/php-string/downloads/detail?name=string.php&can=2&q=
The class supports the extensions mbstring and iconv, and the package PHP-UTF8. It chooses the best available function for each method In addition, it provides many new methods. Some of them are: substringBetween, splice, startWith, endsWith and squeeze. It is also possible to use PHP internal functions, or custom functions, to manipulate the string.
Sample Code:
I have never used this class before but by looking at its documentation it has some pretty interesting methods. capitalize, charAt, compareTo, contains, etc..
It's 2014 and SplString is still not default in PHP. I'd take a look at:
https://github.com/nikic/scalar_objects
It's an extension, so you have to install it.
There is SplString. But it's not available in PHP 5.3 yet. And I doubt it's of much use if it comes around, since it harbors no useful methods whatsoever. Maybe one could built upon it. But then that's only for strings. PHP is built upon scalars, and using it fully object-oriented is not possible at this time.
I'm a bit late to the game, but I was looking for a library just like this and came across this question. After more investigation I found the brilliant danielstjules/Stringy at GitHub.
I've looked over the documentation and the source and it looks pretty damn solid. I'd recommend taking a look at if you want a PHP String Wrapper class to make string manipulation easier. Note that this code is not a PHP extension, meaning there is no native manipulation, it is simply a wrapper.
A few examples:
While Jose Vega suggested a good solution for your problem, there is a farily minor performance issue with the approach. I've whipped up a test for this. While each solution performs well enough for practical purposes, there is a difference.
EDIT: So anyway, since this is neither Ruby, nor Java, nor Smalltalk, you'd be better off using the provided tools than trying to bend the language to meet your expectations. Mostly for performance reasons (because native tools are usually written in C or are low-level enough to have very little performance impact, although it's not always true), and for readability: the community is used to seeing mb_strlen() or strlen(), and other string-related functions.
Here's the code I used for benchmarking:
http://pastebin.com/Q4BfzQtj
Results:
If you wanted to you could create your own String wrapper class that has all the string based methods and calculated attributes that you could possibly want. Edit: In the same way that Java has wrapper classes for some data types.