note 95187 deleted from function.wordwrap by danbrown
| From: | danbrown@php.net | Date: | Thu, 17 Dec 2009 15:46:15 +0000 |
| Subject: | note 95187 deleted from function.wordwrap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164090@lists.php.net to get a copy of this message | ||
Note Submitter: megawilddaddy at gmail dot com
----
ZendFramework filter for force breaking long words
<?php
class My_Filter_BreakLongWords implements Zend_Filter_Interface
{
protected $_maxLength;
public function __construct($maxLength = 10)
{
$this->setMaxLength($maxLength);
}
public function setMaxLength($length)
{
if(!empty($length)) {
$this->_maxLength = $length;
}
}
function filter($text)
{
if (strpos($text, '<') === false) {
$result = $text;
foreach(explode(" ", $text) as $key => $line) {
if (mb_strlen($line, 'UTF-8') > $this->_maxLength) {
$result = str_replace($line, $this->_wordwrap($line, $this->_maxLength,
"­"), $result);
}
}
} else {
$result = preg_replace("/<(.+?)>/", " <$1 > ", $text);
foreach(explode(" ", $result) as $key => $line) {
if (mb_strlen($line, 'UTF-8') > $this->_maxLength) {
$pattern = '/(?!=(<[^<>]+?))('.preg_quote($line,
'/').')(?!([^<>]+?>))/';
$replacement = $this->_wordwrap($line, $this->_maxLength,
"­");
$result = preg_replace($pattern, $replacement, $result);
}
}
}
return $result;
}
// utf-wordwrap
private function _wordwrap($str, $len, $break)
{
return preg_replace("/(.{".$len."})/u", "\${1}".$break, $str);
}
}
?>