note 55516 added to function.wordwrap
| From: | sheiko at cmsdevelopment dot com | Date: | Fri, 05 Aug 2005 13:44:30 +0000 |
| Subject: | note 55516 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93134@lists.php.net to get a copy of this message | ||
When we need to transform a string for the HTML-form range, there is a trouble with HTML/Text
condition of the string. We divide Text string by <BR />, but it isnt appropriated for HTML
string. The breakstring function can solve this problem.
/**
* breakstring() divide HTML-string in order to it can't break design of HTML form
* Author: Dmitry Sheiko (www.cmsdevelopemnt.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 breakstring($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 = "";
$indexes = array(); // Array of stripped HTML-tags indexes
$html = trim($html);
preg_match_all("/<(.*?)>/is", $html, $mathes);
if($mathes[0]) {
foreach($mathes[0] as $block) {
$indexes[$index]["pos"] = strpos($html, $block, $pointer);
$indexes[$index]["tag"] = $block;
$pointer += strlen($block);
$index++;
}
}
$pointer = 0;
$text = preg_replace("/<(.*?)>/is", "", $html);
$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) {
foreach($indexes as $fetch) {
if($fetch["pos"]>$cursor) $fetch["pos"] += strlen($char);
}
}
}
}
$text = join(' ', $words);
}
if($indexes) {
foreach($indexes as $fetch) {
$text = substr($text, 0, $fetch["pos"]).$fetch["tag"].$text = substr($text,
$fetch["pos"]);
}
}
return $text;
}
Yours sincerely,
Dmitry Sheiko,
http://www.cmsdevelopment.com,
senior web developer at Red Graphic Systems (www.redgraphic.com)
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55516
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55516&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55516&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55516&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55516&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55516&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55516&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55516&report=yes
Search -- http://master.php.net/manage/user-notes.php