Extend existing classes like str::newFunction

Hey,
I just wanted to have a function which wraps each character of a string into a given html-tag. I thought it would be a good place to add this to either the str or html class but I couldn’t come up with another solution than cluttering up the global scope. I did the following in a plugin-file:

/**
* This function takes a string and wrappes each character into a given tag.
*
* @return string
*/
function wrapChars($string = '', $tag = 'span')
{
  $newString = '';
  for ($i = 0; $i < str::length($string); $i++) {
    $newString .= html::tag($tag, str::substr($string, $i, 1));
  }
  return $newString;
}

But is there another way of doing this, maybe by adding functionality to existing classes?

Dennis

You can extend the Str class in a plugin

<?php

class StrExt extends Str {

  public static function wrapChars($string = '', $tag = 'span')
  {
    $newString = '';
    for ($i = 0; $i < str::length($string); $i++) {
      $newString .= html::tag($tag, str::substr($string, $i, 1));
    }
    return $newString;
  }
}

Then in your template:

<?= strext::wrapChars('Hello World'); ?>