note 89782 modified in function.wordwrap by danbrown
| From: | danbrown@php.net | Date: | Thu, 02 Apr 2009 13:37:07 +0000 |
| Subject: | note 89782 modified in function.wordwrap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152759@lists.php.net to get a copy of this message | ||
I was wondering about my CMS to break up code but only non-HTML code - I didn't found anything
so I came up with this little solution:
<?php
function wordWrapIgnoreHTML($string, $length = 45, $wrapString = "\n")
{
$wrapped = '';
$word = '';
$html = false;
$string = (string) $string;
for($i=0;$i<strlen($string);$i+=1)
{
$char = $string[$i];
/** 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;
if(strlen($word) > $length)
{
$wrapped .= $word.$wrapString;
$word = '';
}
}
}
if($word !== ''){
$wrapped .= $word;
}
return $wrapped;
}
$str = '<a href="http://www.roberthartung.de">';
$str .= 'Test-STRRRRRRRRRRRRRRRRRRIIIIIIIIIIIIIIIIIIIIIIING</a>';
$str .= '<!-- COMMENT_INSIDE_ANOTHER';
$str .= '_LONG_STRING //-->';
echo wordWrapIgnoreHTML($str, 25, '[BREAK]');
// Output: <a href="http://www.roberthartung.de">
// Test-STRRRRRRRRRRRRRRRRRRI[BREAK]
// IIIIIIIIIIIIIIIIIIIIIING
// </a><!-- ... //-->
?>
[NOTE BY danbrown AT php DOT net: Contains a bug fix provided by (jaw AT condidact DOT dk) on
02-APR-09 to address an issue where "the last word in a non-html-wrapped string will be
omitted."]
--was--
I was wondering about my CMS to break up code but only non-HTML code - I didn't found anything
so I came up with this little solution:
<?php
function wordWrapIgnoreHTML($string, $length = 45, $wrapString = "\n")
{
$wrapped = '';
$word = '';
$html = false;
$string = (string) $string;
for($i=0;$i<strlen($string);$i+=1)
{
$char = $string[$i];
/** 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;
if(strlen($word) > $length)
{
$wrapped .= $word.$wrapString;
$word = '';
}
}
}
return $wrapped;
}
$str = '<a href="http://www.roberthartung.de">';
$str .= 'Test-STRRRRRRRRRRRRRRRRRRIIIIIIIIIIIIIIIIIIIIIIING</a>';
$str .= '<!-- COMMENT_INSIDE_ANOTHER';
$str .= '_LONG_STRING //-->';
echo wordWrapIgnoreHTML($str, 25, '[BREAK]');
// Output: <a href="http://www.roberthartung.de">
// Test-STRRRRRRRRRRRRRRRRRRI[BREAK]
// IIIIIIIIIIIIIIIIIIIIIING
// </a><!-- ... //-->
?>
http://php.net/manual/en/function.wordwrap.php