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]< ?php
/**
* @see Zend_Filter_Interface
*/
require_once 'Zend/Filter/Interface.php';
/**
* Convert a string to titlecase.
*
* Seemingly missing from the core Zend distribution.
*/
class StringToTitle implements Zend_Filter_Interface
{
/**
* Encoding for the input string
*
* @var string
*/
protected $_encoding = null;
/**
* Set the input encoding for the given string
*
* @param string $encoding
* @throws Zend_Filter_Exception
*/
public function setEncoding($encoding = null)
{
if (!function_exists('mb_convert_case')) {
require_once 'Zend/Filter/Exception.php';
throw new Zend_Filter_Exception('mbstring is required for this feature');
}
$this->_encoding = $encoding;
}
/**
* Defined by Zend_Filter_Interface
*
* Returns the string $value, converting characters to titlecase as necessary
*
* @param string $value
* @return string
*/
public function filter($value)
{
if ($this->_encoding) {
return mb_convert_case((string) $value, MB_CASE_TITLE, $this->_encoding);
}
return ucwords((string) $value);
}
}
[/php]