note 96488 deleted from function.wordwrap by danbrown
| From: | danbrown@php.net | Date: | Mon, 01 Mar 2010 17:54:57 +0000 |
| Subject: | note 96488 deleted from function.wordwrap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166516@lists.php.net to get a copy of this message | ||
Note Submitter: krik123 at gmail dot com
----
While looking for the most appropriate smart wordwrappling function over the internet and I found
the solution offered by
r dot hartung at roberthartung dot de and have modified it a little bit in order to fit my needs so
here it is. Hope it will be helpful
function wordWrapIgnoreHTML($string, $length = 45, $wrapString = "<wbr />")
{
$wrapped = '';
$word = '';
$html = false;
$string = (string) $string;
$stringLength = mb_strlen($string);
for($i=0;$i<$stringLength;$i++)
{
$char = mb_substr($string, $i, 1,'utf-8' );
/** HTML Begins */
if($char === '<' )
{
if(!empty($word))
{
$wrapped .= $word;
$word = '';
}
$html = true;
$wrapped .= $char;
}
/** HTML ends */
elseif($char === '>' )
{
$html = false;
$wrapped .= $char;
}
/** If this is inside HTML -> append to the wrapped string */
elseif($html)
{
$wrapped .= $char;
}
/** Whitespace characted / new line */
elseif($char === ' ' || $char === "\t" || $char === "\n" )
{
$wrapped .= $word.$char;
$word = '';
}
/** Check chars */
else
{
$word .= $char;
$wordLength = mb_strlen($word);
if($wordLength > $length && $char == ";" && mb_substr
($string, $i+1, 1)== "&" )
{
$wrapped .= $word.$wrapString;
$word = '';
}
else if ($wordLength > $length && !preg_match("/&.{1,7};/iu",
$word))
{
$wrapped .= $word.$wrapString;
$word = '';
}
else if ($wordLength > $length && !preg_match("/&.{1,7};&/iu",
$word))
{
$wrapped .= $word.$wrapString;
$word = '';
}
}
}
if($word !== '')
{
$wrapped .= $word;
}
return $wrapped;
}