note 89782 added to function.wordwrap
| From: | r dot hartung at roberthartung dot de | Date: | Mon, 23 Mar 2009 12:26:32 +0000 |
| Subject: | note 89782 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152320@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 = '';
}
}
}
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><!-- ... //-->
?>
----
Server IP: 212.124.37.9
Probable Submitter: 95.88.195.1
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit -- https://master.php.net/note/edit/89782
Del: integrated -- https://master.php.net/note/delete/89782/integrated
Del: useless -- https://master.php.net/note/delete/89782/useless
Del: bad code -- https://master.php.net/note/delete/89782/bad+code
Del: spam -- https://master.php.net/note/delete/89782/spam
Del: non-english -- https://master.php.net/note/delete/89782/non-english
Del: in docs -- https://master.php.net/note/delete/89782/in+docs
Del: other reasons-- https://master.php.net/note/delete/89782
Reject -- https://master.php.net/note/reject/89782
Search -- https://master.php.net/manage/user-notes.php