Object oriented strings / numbers in PHP?

I looked at Ruby and it has a very good OO structure, unlike PHP with C-like string functions. I was wondering if there is an extension that makes strings into objects so you can use them like this:

$str = "sometext";
echo "len:" . $str->length; //would print 'len: 8'
+5
source share
6 answers

Take a look at this ...

http://code.google.com/p/php-string/downloads/detail?name=string.php&can=2&q=

mbstring iconv PHP-UTF8. . , . : substringBetween, , startWith, endWith . PHP .

:

<?php

include('string.php');

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

?>

, , , . capize, charAt, compareTo, ..

+7

, - String , , , . Edit: , Java - .

+2

, . danielstjules/Stringy GitHub.

, . , , PHP String Wrapper . , PHP, , .

Stringy

PHP . PHP 5.3+, PHP 7 HHVM.

:

s('Upper Camel-Case')->upperCamelize(); // 'UpperCamelCase'
s('What are your plans today?')->truncate(19, '...'); // 'What are your pl...'
s('foo & bar')->containsAll(['foo', 'bar']); // true
s('Ο συγγραφέας είπε')->countSubstr('α'); // 2
+2

, . . , .

EDIT: , Ruby, Java, Smalltalk, , . ( C , , ) : mb_strlen() strlen(), .

, :

http://pastebin.com/Q4BfzQtj

:

====> Test run 0
And here are the results:
Test with {} = 0.00097203254699707
Test with strlen() = 0.0030488967895508
Test with mb_strlen() = 0.0031669139862061
Test with String1 object = 0.012485027313232
Test with String object = 0.036020040512085


====> Test run 1
Test with {} = 0.00095200538635254
Test with strlen() = 0.0029759407043457
Test with mb_strlen() = 0.0031669139862061
Test with String1 object = 0.012346982955933
Test with String object = 0.036028146743774


====> Test run 2
Test with {} = 0.0009617805480957
Test with strlen() = 0.0029959678649902
Test with mb_strlen() = 0.0031518936157227
Test with String1 object = 0.012416124343872
Test with String object = 0.037784099578857


====> Test run 3
Test with {} = 0.00081610679626465
Test with strlen() = 0.0025439262390137
Test with mb_strlen() = 0.0027410984039307
Test with String1 object = 0.010634183883667
Test with String object = 0.030903100967407


====> Test run 4
Test with {} = 0.00081205368041992
Test with strlen() = 0.0025379657745361
Test with mb_strlen() = 0.0027129650115967
Test with String1 object = 0.010583162307739
Test with String object = 0.031081914901733


====> Test run 5
Test with {} = 0.000823974609375
Test with strlen() = 0.0025639533996582
Test with mb_strlen() = 0.0026860237121582
Test with String1 object = 0.010586023330688
Test with String object = 0.030833959579468   
+1

SplString. PHP 5.3. , , , . , . . PHP , - .

0

2014 SplString - PHP. :

https://github.com/nikic/scalar_objects

, .

0

All Articles