This topic is locked

Manually Truncate text in fields

6/28/2011 4:58:20 PM
PHPRunner Tips and Tricks
F
FunkDaddy author

I came across the need to truncate fields in varying lengths in the same form. Therefore, the "misc" tab in PHPR wasn't enough to meet my needs given that it applies the "truncate large text fields )more...link)" option to all fields on a form.
You simply add the following code to the "custom" view as option of whatever field you want to control:



//Truncate field text

$lenght_limit = 25; //limit to this number of characters

$actual_length = strlen($value); //count characters in the $value

$mytext = $value;

$mytext = substr($mytext,0,$lenght_limit ); //picks up characters from left to right starting at position zero all the way to 25
//Truncate if exceeds $chars value!

if($actual_length <= $length_limit){

$value = $mytext;

} else {

$mttext = $mytext."...";

$value = $mytext;

}


I got some inspiration from the following article on this site: http://www.the-art-of-web.com/php/truncate/
As you can see, there are many different options for truncating based on your needs (by line breaks, by period, etc). However, all I needed was a simple character count, so I wrote simpler code.
I originally tried using CSS to handle this but came across issues rendering on different browsers... thus word-wrap, whitespace, overflow, ellipsis, etc didn't do the trick for me on all browsers; thus I resorted to coding in php.
Also, worth mentioning is that this code doesn't interfere with the results of a lookup wizard field value as provided by PHPR, so you can easily use it along with those field types as well.
Cheers,