Kristina Brooks Kristina Brooks - 5 months ago 36
PHP Question

Object orientated strings / numbers in PHP?

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...

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:



$str = new String('sometext');
echo $str->length; //prints 8
echo $str->getLength(); //prints 8


I have never used this class before but by looking at its documentation it has some pretty interesting methods. capitalize, charAt, compareTo, contains, etc..