StringToTitle filter

I like the filtering capabilities of the Zend Framework, but for some reason there doesn't seem to be a string to title case filter (though there is a string to upper and string to lower). So here it is:

PHP:
  1. <?php
  2.  
  3. /**
  4. * @see Zend_Filter_Interface
  5. */
  6. require_once 'Zend/Filter/Interface.php';
  7.  
  8. /**
  9. * Convert a string to titlecase.
  10. *
  11. * Seemingly missing from the core Zend distribution.
  12. */
  13. class StringToTitle implements Zend_Filter_Interface
  14. {
  15.     /**
  16.      * Encoding for the input string
  17.      *
  18.      * @var string
  19.      */
  20.     protected $_encoding = null;
  21.  
  22.     /**
  23.      * Set the input encoding for the given string
  24.      *
  25.      * @param  string $encoding
  26.      * @throws Zend_Filter_Exception
  27.      */
  28.     public function setEncoding($encoding = null)
  29.     {
  30.         if (!function_exists('mb_convert_case')) {
  31.             require_once 'Zend/Filter/Exception.php';
  32.             throw new Zend_Filter_Exception('mbstring is required for this feature');
  33.         }
  34.         $this->_encoding = $encoding;
  35.     }
  36.  
  37.     /**
  38.      * Defined by Zend_Filter_Interface
  39.      *
  40.      * Returns the string $value, converting characters to titlecase as necessary
  41.      *
  42.      * @param  string $value
  43.      * @return string
  44.      */
  45.     public function filter($value)
  46.     {
  47.         if ($this->_encoding) {
  48.             return mb_convert_case((string) $value, MB_CASE_TITLE, $this->_encoding);
  49.         }
  50.         return ucwords((string) $value);
  51.     }
  52. }

0 Responses to “StringToTitle filter”


  1. No Comments

Leave a Reply

You must login to post a comment.




Mp3 sparks Allofmp3