note 95187 modified in function.wordwrap by thiago
| From: | thiago@php.net | Date: | Thu, 17 Dec 2009 12:37:02 +0000 |
| Subject: | note 95187 modified in function.wordwrap by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164081@lists.php.net to get a copy of this message | ||
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);
}
}
?>
--was--
ZendFramework filter for force breaking long words
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);
}
}
http://php.net/manual/en/function.wordwrap.php