Re: Little Problem :)
| From: | CPT John W. Holmes | Date: | Tue, 11 Mar 2003 21:47:56 +0000 |
| Subject: | Re: Little Problem :) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-139189@lists.php.net to get a copy of this message | ||
> 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...