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
[EMAIL PROTECTED]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]