views:

59

answers:

3

I have text with minimal tags like 'a','b','i' etc inside it. Now i want to truncate the text to 100 chars and still want to maintian the formatting. I don;t want to strip the tags off from the text.

Is it possible? how can it be done.

A: 

You would get the text on it's own and truncate it, and then put it back into the tags. Sorry, I don't have an example.

Sam
+1  A: 

An easy way would be to do it using javascript, but a solution using php is possible if you got the tags + contents ont the server side. All you need to do is to parse your tag+content so that you separate the tags from the content and then truncate your content text to 100 characters. Ad the separated tags you still have in a varable and there you go.

Very simple example:

$fulltag_as_string = '<b>This is my text ... blah, blah .. with length more than 100 characters</b>';

$arr = split(">",$fulltag_as_string);
$arr2 = split("<",$fulltag_as_string);
$arr3 = split("<",$arr[1]);

$truncated_text = strlen($arr3[0] > 100) ? substr($arr3[0],0,100) : $arr3[0];

echo $resulting_tag = $arr[0].">".$truncated_text."<".$arr2[2];
Thariama
+1  A: 

I saw this answered in another question here, The link provided was http://snippets.dzone.com/posts/show/7125 by Dennis Pedrie on http://stackoverflow.com/questions/2974833/how-to-truncate-html-to-certain-number-of-characters/2975162#2975162

Hope this helps, its basically a short class that will do what you want with a very simple call, if you scroll down some people have made a few improvements too.

Regards Luke

Luke