Kristina Brooks Kristina Brooks - 3 months ago 16
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'

Answer

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:

<?php

include('string.php');

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

Comments