note 96488 added to function.wordwrap

From: Date: Mon, 01 Mar 2010 17:51:41 +0000
Subject: note 96488 added to function.wordwrap
Groups: php.notes 
Request: Send a blank email to php-notes+get-166515@lists.php.net to get a copy of this message
While looking for the most appropriate smart wordwrappling function over the internet and I found the solution offered by r dot hartung at roberthartung dot de and have modified it a little bit in order to fit my needs so here it is. Hope it will be helpful function wordWrapIgnoreHTML($string, $length = 45, $wrapString = "<wbr />") { $wrapped = ''; $word = ''; $html = false; $string = (string) $string; $stringLength = mb_strlen($string); for($i=0;$i<$stringLength;$i++) { $char = mb_substr($string, $i, 1,'utf-8' ); /** 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; $wordLength = mb_strlen($word); if($wordLength > $length && $char == ";" && mb_substr ($string, $i+1, 1)== "&" ) { $wrapped .= $word.$wrapString; $word = ''; } else if ($wordLength > $length && !preg_match("/&.{1,7};/iu", $word)) { $wrapped .= $word.$wrapString; $word = ''; } else if ($wordLength > $length && !preg_match("/&.{1,7};&/iu", $word)) { $wrapped .= $word.$wrapString; $word = ''; } } } if($word !== '') { $wrapped .= $word; } return $wrapped; } ---- Server IP: 69.147.83.197 Probable Submitter: 92.245.111.254 ---- Manual Page -- http://www.php.net/manual/en/function.wordwrap.php Edit -- https://master.php.net/note/edit/96488 Del: integrated -- https://master.php.net/note/delete/96488/integrated Del: useless -- https://master.php.net/note/delete/96488/useless Del: bad code -- https://master.php.net/note/delete/96488/bad+code Del: spam -- https://master.php.net/note/delete/96488/spam Del: non-english -- https://master.php.net/note/delete/96488/non-english Del: in docs -- https://master.php.net/note/delete/96488/in+docs Del: other reasons-- https://master.php.net/note/delete/96488 Reject -- https://master.php.net/note/reject/96488 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166515) next »