note 51861 added to function.wordwrap
| From: | hokus_123 at hotmail dot com | Date: | Wed, 13 Apr 2005 07:31:08 +0000 |
| Subject: | note 51861 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-87941@lists.php.net to get a copy of this message | ||
for ex. msgbrds where HTML hyberlinks occur, wrapping words will devide long URLs and make them
invalid. Here is a "real" wordwrap that does not break up HTML hyberlinks.
<?PHP
function mywordwrap($str, $max = 75, $break = ' ') {
// create array by deviding at each occurrence of "<a"
$arr = explode('<a', $str);
// break up long words in $arr[0] since
// it will never contain a hyberlink
$arr[0] = preg_replace('/([^\s]{'.$max.'})/i',"$1$break",$arr[0]);
// run loop to devide remaining elements
for($i = 1; $i < count($arr); $i++) {
// devide each element in $arr at each occurrence of "</a>"
$arr2 = explode('</a>', $arr[$i]);
// break up long words in $arr2 that does not
// contain hyberlinks
$arr2[1] =
preg_replace('/([^\s]{'.$max.'})/i',"$1$break",$arr2[1]);
// rejoin $arr2 and assign as element in $arr
$arr[$i] = join('</a>', $arr2);
}
// rejoin $arr to string and return it
return join('<a', $arr);
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51861
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51861&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51861&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51861&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51861&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51861&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51861&report=yes
Search -- http://master.php.net/manage/user-notes.php