How to truncate an html text and still maintain the format, in PHP
I have text with minimal tags l开发者_运维技巧ike '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.
I saw this answered in another question here, The link provided was http://snippets.dzone.com/posts/show/7125 by Dennis Pedrie on How to truncate HTML to certain number of characters?
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
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];
精彩评论