Re: Cutting long strings for tables

From: Date: Fri, 02 Mar 2001 04:11:00 +0000
Subject: Re: Cutting long strings for tables
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-42280@lists.php.net to get a copy of this message
Juan, I'm not sure, but isn't there a wrap function? ----- Original Message ----- From: Juan Germano <droctulft@yifan.net> To: <php-general@lists.php.net> Sent: Thursday, March 01, 2001 6:29 PM Subject: [PHP] Cutting long strings for tables > Hi. I use this function with the strings I'll be showing in tables to cut > strings too long without spaces. > To make it clear: > > <table > width="50"><tr><td><?=$str?></td></tr></table> > > The table will be 50px width if $str isn't too long. Let's say: > $str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" > The table goes to hell. > So: > function cut_text($res,$long=15,$max=20){ > if ($max<=$long){$max=$long+2;} > $ant=""; > while ($res!=$ant){ > $ant=$res; > $res=addslashes($res); > $res= preg_replace ("/(\w){".$max.",}/im","'.substr (\\0,0,$long).' > '.substr (\\0,$long,strlen('\\0')-$long).'",$res); > $res=" return '$res';"; > $res=eval($res); > } > return $res; > } > > For example: > $str="Hello Howwwwwwwwwwwww arrrrreeeeeee yyyooooooouuuuuu"; > echo cut_text($str,7,10); > Results in: > Hello Howwwww wwwwwwww arrrrre eeeeee yyyoooo ooouuuuuu > > The idea is: any word longer than 20 chars will split into a chunk of 15 > and a chunk of 5. If you have a 45 chars string, it will leave you with 20, > 20 and 5. > It works fine with simple text, but with characters like ", / , $, etc. > sometimes I have problems. I tried with quotemeta instead of addslashes but > the problem persists. Anyone have an idea?. If you want to test the code, > just try with > Testing \" quotes > > Thanks in advance > > Juan I. Germano > droctulft@yifan.net > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#42280) next »