How to fine tune a block of user generated HTML while maintaining formatting?

I would like to create a typical preview snippet using the [read more] link. The problem is that the content that I would like to use for SubString () contains text and html written by the user with the WYSIWYG editor.

Of course, I check that the string is not empty or empty, and then SubString (), the problem is that I can finish breaking the html tags, discarding the rendering of the whole site.

The WYSIWYG editor does not seem to produce perfectly formatted HTML, and many times it seems to use tags <br />instead <p></p>, etc .... in principle, I cannot rely on well-formed tags, etc ..

My workaround was to simply remove all the HTML and fine-tune the remaining text. This works, but loses any formatting that was in HTML.

What is the best SubString () method in a block of unconfigured HTML while retaining HTML that will not disrupt site rendering?

+3
source share
1 answer

How about repeating this substring, looking for any private tags and keeping them in the list, removing any that are closed? Then you can add closing tags for any open tags from the list (in reverse order), which will give you useful html ...

0
source

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


All Articles