Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - wordwrap with html

The problem is simple:

Using tinyMCE, I am storing html data into the database. Then at the home page, I am showing 5 latest posts I've inserted into the database. I want to crop each of these posts to the specified length if they exceed it and put a continue reading link (can be a soft crop here, doesn't have to be rigid).

It would be easy to crop the string with php's wordwrap function but since the string is composed of html I don't wanna ruin the html code by cropping it from a wrong place.

So the question is: Is there an easy way to crop this, (can be a css, javascript solution as well) or do I have a write a long function with lots of checks to implement such a basic feature? I was thinking to use the DOM class but before creating function I just wanted to ask you guys.

EDIT

Styling is essential to me. So there is no possibility to strip the tags.

Thanks in advance.

like image 325
Savas Vedova Avatar asked Nov 03 '22 01:11

Savas Vedova


1 Answers

One possible solution could be to strip tags, if the styling of this HTML is not vital, so you can cut wherever you want: http://php.net/manual/es/function.strip-tags.php

Another solution for CSS3 browsers, would be to use text-overflow, read more about this: http://davidwalsh.name/css-ellipsis

like image 57
netadictos Avatar answered Nov 08 '22 09:11

netadictos