note 92879 deleted from function.wordwrap by danbrown
| From: | danbrown@php.net | Date: | Wed, 12 Aug 2009 21:58:25 +0000 |
| Subject: | note 92879 deleted from function.wordwrap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-159414@lists.php.net to get a copy of this message | ||
Note Submitter: sbm007 at gmail dot com
----
I modified the wordwrap function submitted by "tylernt at gmail dot com"' to suit my
needs.
This is what I came up with:
<?php
function BreakWord($Message, $Length)
{
$ReturnMessage = "";
$CurrentLength = 0;
$Length = strlen($Message);
for ($i = 0; $i < $Length; $i++)
{
$CurrentCharacter = substr($Message, $i, 1);
if ($CurrentCharacter == " ")
$CurrentLength = 0;
else
$CurrentLength++;
$ReturnMessage = $ReturnMessage . $CurrentCharacter;
if ($CurrentLength > $Length)
{
$ReturnMessage = $ReturnMessage . "​";
$CurrentLength = 0;
}
}
return $ReturnMessage;
}
?>
This is a bit different to wordwrap. Allow me to explain:
If a word exceeds length $Length, it will insert a zero-width space character at that point. This
will cause browsers to wrap the text onto the next line (exactly the same as the CSS3 property:
word-wrap: break-word;)
Usage is simple:
BreakWord($Message, $Length)
$Message is your text/string
$Length is maximum length before a word is wrapped onto next the next line. $Length should generally
speaking be the amount of capital double-u's (W) can fit on one line of your page. (Or inside a
table/div/etc)
Hope this helps, please do not hesitate to contact me if you have a problem.