Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to "truncate" a blob field

Status
Not open for further replies.

irbk

MIS
Oct 20, 2004
578
US
I'm looking for a "function" or something in PHP. Long story short, we are doing a newsletter. I want the front page of the newsletter to be the first 200 characters of each newsletter article. Looking for a "trim" or "truncate" function. That way I can do something like "trim($row,200)" and it would trim "$row" to 200 characters. I searched on PHP.net and I know the actual PHP "trim" function seems to remove characters from either the from the front (ltrim) rear (rtrim) or both (trim) but dosen't seem to limit the amout of characters returned. I had less luck understanding what the OCI-LOB->Truncate functions were. Seem to have to do with file length, not necessarly characters.

Any help would be appreshated. Thanks!
 
How about:

Code:
$shortstring=substr($mystring,0,200);

This will assign to $shortstring 200 characters of the string. $mystring starting from the character 0. That is the first character in the string.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I did find this nice little function that seems to do the trick.

*****CODE****
function nicetrim ($s) {
// limit the length of the given string to $MAX_LENGTH char
// If it is more, it keeps the first $MAX_LENGTH-3 characters
// and adds "..."
// It counts HTML char such as á as 1 char.
//

$MAX_LENGTH = 10;
$str_to_count = html_entity_decode($s);
if (strlen($str_to_count) <= $MAX_LENGTH) {
return $s;
}

$s2 = substr($str_to_count, 0, $MAX_LENGTH - 3);
$s2 .= "...";
return htmlentities($s2);
}
*****End Code*****

Works good but I was really hoping for a "built in" function that didn't require all the hubub.
 
I'm sort of leaning the other way, using a substring function on the database server. I don't like to fetch data I'm not going to use.

If you say OCI, then you might ask in an Oracle forum how to write a query that will return only the first x characters from a blob.



Want the best answers? Ask the best questions! TANSTAAFL!
 
vacunita, yours did the trick too and is a lot shorter then the function I found. However, I do have to consider that the substr could cause the sentance to terminate right in the middle of an &amp; or something with the resulting text not looking too pretty... Hmmm... Have to think about that.

Thanks for the post!
 
Well, after a little bit if playing around, since our news letters contain lots of "&amp;" in the code (company name has an & in it) I've decided to stick with the function as the substr could terminate in the middle of an &amp; causing an output of "&am" (depending on where the substr ends) to the screen. Thanks a ton for your post!
 
You can always add 3 periods "..." at the end to simbolize its just a fragment.

But its going to be really hard to get it to truncate at the end of a sentence. Usually text previews don't necessarily have to end nicely as it is already known that they are not the complete text.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Finally got around to trying the NiceTrim function above. Problem is, the code that it returns is not being parsed correctly by HTML. For example rather then displaying on the screen

Bold text here
it displays
<strong>Bold text here</strong>

Any ideas as to why?
 
change the last line to
Code:
return $s2;
i.e. get rid of the htmlentities.
 
Thanks for the post jpadine. I figured out that it was the htmlentites that was causing the issue. I did post it but something happed either to my connection or to the website at the time that I posted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top