How to crop html text and maintain format in PHP

I have text with minimal tags like "a", "b", "i", etc. inside of it. Now I want to truncate the text to 100 characters and still want to format maintian. I do not want to separate tags from text.

Is it possible? How can I do that.

+3
source share
2 answers

I saw that this is the answer to another question here, the link was http://snippets.dzone.com/posts/show/7125 Dennis Pedrie on How to truncate HTML by a certain number of characters?

, , , , , , , .

+1

javascript, php , + . , , + , , 100 . , , .

:

$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];
+1

Source: https://habr.com/ru/post/1751520/


All Articles