Re: Little Problem :)

From: Date: Tue, 11 Mar 2003 21:16:10 +0000
Subject: Re: Little Problem :)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-139184@lists.php.net to get a copy of this message
At 21:32 11.03.2003, David Soler said: --------------------[snip]-------------------- >user writes: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" > >in the web must appear something like: > > table width > |--------| > aaaaaa > aaaaaa > aaaaaa > aaaaaa --------------------[snip]-------------------- 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; } $test = 'abc defghijklmno pqrstuvwxyz' . 'abcdefghijklmnopqrstuvwxyz' . 'abc defghijklmno pqrstuvwxyz' . 'abcdefghijklmnopqrstuvwxyz' . 'abc defghijklmno pqrstuvwxyz' . 'abcdefghijklmnopqrstuvwxyz'; echo $test, '<hr>', nl2br(make_maxlen($test, 25)); -------------------------------------------------------- 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. -- >O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/

« previous php.general (#139184) next »