note 55964 added to function.wordwrap
| From: | sheiko at cmsdevelopment dot com | Date: | Fri, 19 Aug 2005 08:45:17 +0000 |
| Subject: | note 55964 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93878@lists.php.net to get a copy of this message | ||
<?PHP
/**
* wrapstring() wraps HTML-string in order to it can't able to break design of HTML form
* The function create index list of all HTML tags of source and restore HTML-constructions after
text transformation
* So the function wraps strings with HTML tags (e.g. <A HREF="">) to be displayed
correctly
*
* Author: Dmitry Sheiko (www.cmsdevelopment.com)
*
* @param $html Text to dividing.
* @param $max Max word size.
* @param $char character. Defaults to <BR />.
* @return The text with inserted $char character.
**/
function wrapstring($html, $max, $char = "<BR />") {
$words = array(); // Array of text words
$word = ""; // A word of words array
$block = "";
$newword = "";
$pointer = 0;
$cursor = 0;
$index = 0;
$fetch = 0;
$text = "";
$lens = "";
$indexes = array(); // Array of stripped HTML-tags indexes
$text = $html = preg_replace("/<br\s?\/?>/is", "<br /> ",
trim($html));
// Indexing model
// text<tag>text<tag>text
// [len0][tag0][len1][tag1][len2]
preg_match_all("/<(.*?)>/is", $html, $mathes);
if($mathes[0]) {
foreach($mathes[0] as $block) {
$indexes[$index]["len"] = ($cursor = strpos($html, $block, $cursor)) - $pointer;
$indexes[$index]["tag"] = $block;
$pointer = $cursor + strlen($block);
$index++;
}
}
$text = preg_replace("/(<.*?>)/is", "", $html);
$pointer = 0;
$cursor = 0;
$words = split(' ', $text);
if($words) {
foreach ($words as $index => $word) {
$cursor += strlen($word);
if(strlen($word)>$max) {
while($pointer < strlen($word)) {
$newword .= substr($word, $pointer, $max).$char;
$pointer += $max;
}
$words[$index] = $newword;
if($indexes) {
$lens = 0;
foreach($indexes as $key => $fetch) {
$lens += $fetch["len"];
if($lens > $cursor) { $indexes[$key]["len"] += (strlen($newword)-strlen($word));
break; }
}
}
}
}
$text = join(' ', $words);
}
$index = 0;
$cursor = "";
$words = array();
foreach($indexes as $key => $fetch) {
$words[$index++] = substr($text, $cursor, $fetch["len"]);
$words[$index++] = $fetch["tag"];
$cursor += $fetch["len"];
}
$html = join("", $words);
return $html;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55964
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55964&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55964&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55964&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55964&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55964&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55964&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55964&report=yes
Search -- http://master.php.net/manage/user-notes.php