> You can use a regular expression for this task. This is what I use for my
> CMS routines:
>
> --------------------------------------------------------
> function make_maxlen($string, $maxlen)
> {
>     $re = '/(.*?)([^\s]{' . $maxlen . ',})(.*)/';
>     $out = null;
>     while (preg_match($re, $string, $aresult)) {
>         $out .= $aresult[1]; // pre-match
>         $out .= substr($aresult[2], 0, $maxlen) . "\n";  // first n
> characters of matching long-word
>         $string = substr($aresult[2], $maxlen) . $aresult[3]; // stuff
back
> reminder
>     }
>     $out .= $string;
>     return $out;
> }
[snip]
> Note that the make_maxlen function returns strings split up merely with a
> newline character, so use nl2br to create a forced newline. There's no
> drawback in not using nl2br (not adding '<br />'s) since the inserted
> newline character will be visible as whitespace and allow the user agent
to
> properly reformat the text within the area.

Just so you feel properly schooled, you could use this:

function make_maxlen($string,$maxlength=10,$sep="\n")
{ return preg_replace("/(\S{".$maxlength."})/","$1".$sep,$string); }

:)

---John Holmes...


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to