note 55516 deleted from function.wordwrap by nlopess

From: Date: Fri, 05 Aug 2005 13:55:57 +0000
Subject: note 55516 deleted from function.wordwrap by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-93135@lists.php.net to get a copy of this message
Note Submitter: sheiko at cmsdevelopment dot com ---- 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 isn’t 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)

« previous php.notes (#93135) next »