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?

3
  • 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? Commented Sep 6, 2012 at 23:03
  • 2
    Have you looked at this? php.net/manual/en/book.tidy.php Commented Sep 6, 2012 at 23:03
  • My mistake*, the WYSIWYG is generating valid syntax, but some times the user may do something that, directly, cut html tags. Commented Sep 7, 2012 at 1:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The wpautop with a mix of force_balance_tags was just what I wanted, thx=)
0

You can try HTML Tidy.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.