1

I need to clean an html string but I need to keep a custom tagin it , like :

<CUSTOM_TAG/>

I use 'tidy_repair_string()' php function.

$str = '<div><CUSTOM_TAG/><br><span>my little html</span></div>';
$tidy_config = array();
$tidy_text = tidy_repair_string($str, $tidy_config, 'utf8');

I didn't find any Tidy options which can help me. Any idea ?

In advance, Thanks.


I'VE FOUND THE SOLUTION :

The Tidy option you need to use is : " 'input-xml' => true ". Thank you all for your investigation !!

4
  • in your example, the desired output would be <CUSTOM_TAG/>my little html or just <CUSTOM_TAG/>? Commented May 24, 2013 at 9:19
  • This HTML has to be valid and other stuff. So I need to keep my CUSTOM_TAG into the tidy HTML. Commented May 24, 2013 at 9:52
  • then please provided a couple of examples of input => desired output, please. Commented May 24, 2013 at 9:55
  • Input : '<div><h1><CUSTOM_TAG id="487"/><br><span>my little html</span></div><a/>' // Ouput : '<div><CUSTOM_TAG id="487"/><br><span>my little html</span></div>' Commented May 24, 2013 at 10:06

3 Answers 3

2

The Tidy option you need to use is : " 'input-xml' => true ". Thank you all for your investigation !!

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

Comments

0

strip_tags( ) should be able to do what you want. You can give it a list of tags to skip.

Example:

$str = '<div><CUSTOM_TAG/><br><span>my little html</span></div>';
$tidy_text = strip_tags( $str, "<CUSTOM_TAG>" );

1 Comment

As I've said on top of the page, I need to tidy my HTML + keep custom tags.
0

You need to teach Tidy that the <CUSTOM_TAG> is valid by specifying the new-blocklevel-tags configuration option. You probably also need to add it to new-empty-tags to make it accept it without content or attributes, it will probably strip it away otherwise.

4 Comments

I've tried to do it: $tidy_config = array('new-blocklevel-tags' => 'CUSTOM_TAG', 'new- empty-tags' => 'CUSTOM_TAG'); Seems no working...
If I try it online with infohound.net/tidy it only accepts the custom tag if specified in lower case. When I enter CUSTOM_TAG in caps it still strips it.
There's also a surplus space in new- empty-tags in your code sample btw.
Yes, it was a wrong copy / paste. In my code there's no space. But thanks for your notice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.