1

I have a string that contain data formatted through a WYSIWYG editor. But the generated HTML code is not very well formatted. I would like something as the "the_content()" wordpress function, that delivers a string like this:

Lorem ipsum dolor sit amet, <strong>not properly closed

Another paragraph without p, lorem ipsum dolor m nonummy.

To somethig like this:

<p>Lorem ipsum dolor sit amet, <strong>not properly closed</strong></p>

<p>Another paragraph without p, lorem ipsum dolor m nonummy.</p>

I look up to the wordpress core but without much success (found a force_balance_tags() function, but don't seems to be what I want).

So... do you guys know some php class/library/code snippet that does that?

lucaswxp
  • 2,031
  • 5
  • 23
  • 34
  • 2
    I think you're going about this problem wrong. The question is not how to fix the badly formatted code, the problem is why is your WYSIWYG editor not returning well formatted code? – Erik Sep 06 '12 at 23:03
  • 2
    Have you looked at this? http://php.net/manual/en/book.tidy.php – Brad Sep 06 '12 at 23:03
  • My mistake*, the WYSIWYG is generating valid syntax, but some times the user may do something that, directly, cut html tags. – lucaswxp Sep 07 '12 at 01:38

2 Answers2

0

You can try HTML Tidy.

Also have a look at this question: Tidying PHP and HTML Code?

Community
  • 1
  • 1
nageeb
  • 2,002
  • 1
  • 13
  • 25
0

Wordpress has a function called wpautop which converts double line-breaks to paragraph elements.

I would not suggest using a library for correcting the users wrongly formatted HTML though. You would be forced to guess the correct intepretation. (like old browsers did back in the days, and that was not exactly a success).

As an example, Where should the closing </strong> tag in your code be placed? No one knows!

I would rather look for (or create) a method to check for unvalid HTML and then notify the user, and let them correct it.

Community
  • 1
  • 1
Frederik Wordenskjold
  • 10,031
  • 6
  • 38
  • 57