String class

UPDATE: since kalfu bit the dust the svn repo is no more...however I kept a copy of the class around for those interested in messing with it - and updated the url

Yes yes, overloading "basic" php types is evil. However, it is the EASIEST and BEST way to handle multibyte or translated strings.

So my rather cumbersome three classes to handle translation and charsets is now ONE class. When the rest of the magic __toString stuff goes into php (estimated for 5.2, which I wouldn't know if I didn't read internals religiously) it makes it even easier to use.

Kal_String is the class itself. Basically it has TWO constructors - because there are a series of static settings and two static methods that deal with things like a default charset to use for all strings and a default language to look for. The language searching is set up with a callback - so you can write your own class using gettext or including straight php files or whatever you want. You can even manually load in translation strings for individual string instances if you're so inclined.

The basic usage looks like this - I threw in the header cause I was testing this in a browser and am lazy

first of all, you're going to need the kal_exception and kal_filter classes in addition to the kal_string class - sorry but centralized data cleaning and error handling are IMPORTANT

[php]
header('Content-Type: text/plain');
include('exception.class.php');
include('filter.class.php');
include('string.class.php');

// basic string usage
$string = new Kal_String('My string');
echo $string,"\n";

//string usage with replacement array
$string = new Kal_String('My %s string %s', array(3, 'hello'));
echo $string,"\n";
[/php]

This should give you
My string My 3 string hello

All right, now let's write a stupid little function to register as a search and play with changing the language

[php]
function trans_lookup($string, $lang)
{
$array = array(
'en' => 'I like cats',
'fr' => 'J'aime des chats',
'de' => 'Ich mag Katzen',
'es' => 'Tengo gusto de gatos',
'it' => 'Gradisco i gatti',
);
if(isset($array[$lang]))
{
return $array[$lang];
}
return $string;
}

//string usage with search set
$string = new Kal_String('I like cats');
Kal_String::setSearch('trans_lookup');

echo $string,"\n";

$string->lang = 'fr';
echo $string,"\n";

$string->lang = 'de';
echo $string,"\n\n";
[/php]

Which will display

I like cats J'aime des chats Ich mag Katzen

One thing to note - notice that I'm not using concatenation (.) but rather a comma between the echo and newline. Right now
__toString magic only works with echo and print (annoying) but that's supposed to be fixed in 5.2... until then you can use $string->__toString() to get the text.

There are also "overloaded" string manipulation functions so if you're using mb_string or iconv you can get REAL strlen and strpos and other fun stuff. Notice this is overkill if you're not using a multibyte character set like utf-8;

[php]
// we're killing off the search function - this WILL STAY ACTIVE for ALL INSTANCES on the SAME SCRIPT until you reset/change it
Kal_String::setSearch(NULL);
$string = new Kal_String('hello & there');
echo $string,"\n";

echo 'String Overloads'."\n";
echo $string->strtolower()."\n";
echo $string->strtoupper()."\n";
echo $string->ucwords()."\n";
echo $string->ucfirst()."\n";
echo $string->substr(0, 6)."\n";
echo $string->strlen()."\n";
echo $string->bytelen()."\n";
echo $string->strpos('th')."\n";
echo $string->strrpos('th')."\n";
echo $string->charencode()."\n";
echo $string->chardecode()."\n";
echo $string->entitydecode()."\n";
echo $string->entityencode()."\n";
[/php]

Result?
hello & there String Overloads hello & there HELLO & THERE Hello & There Hello & there hello 15 15 10 10 hello < & there hello & there hello & there hello < & there

yes, I know, nothing exciting - but it works

you can also manipulate the charset of the current string - but depending on the extension you're using the current choices are limited - that may change in the future

[php]
$string->charset = 'ASCII';
echo $string;
[/php]

This would be more exciting if I had more exciting string examples, I know...

Bottom line? A nifty class to use. And I'm getting the roadmap (something on my poor new years resolution list) to a useful state. Wahoo!

Comments

jeff

Does the Kal_String class still exist anywhere?

2007-02-20 11:38 pm

auroraeosrose

Yes, thanks for asking...I updated the url and added a note to the post

2007-02-24 7:26 pm

Post a Reply