I'm using text editors for entering the posts on my WEB (this one). The text is well formated (I have images, text is styled), so the post in my database are stored in their HTML format. For example this is how a post with an image and some text will be stored in database:
<p><img alt="image" src="./img/news-img.jpg" style="height1:311px; width1:1157px" /></p>
<p style="text-align: justify;"><span style="color:rgb(112, 112, 112); font-family:opensansregular; font-size:14px">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span></p>
And it will look something like this when is displayed. I'm using only PHP without any framework. Now I want to have only a few words on the homepage for each post and to add a button "Read More" that will lead to the page with all the content of the post. I have tried (but I expected that it won't work) to substring the text. It is not working because the post is taken as normal string (php can not see that the tags are not closed) and If I specify to take only first 200 characters and in the mean time there is <i>
tag, if the closing </i>
tag is not in those 200 characters, all the text below my post is in italic. Or another scenario, if is split with taking 200 characters the image is not shown because the text is not taken completely.
Is there a way that I could convert the content somehow, and when taking the first 200 charterers (this number is just an example and no need to be fixed for all the post) from the database, all the tags to be closed in the mean time and the content below that post to be normal?