Question

So I use tidy to format html nicely:

$html = 'one line html with many tags';
$config = array(
   'indent'         => true,
   'indent-spaces'  => 2,
   'show-body-only' => true,
   'wrap'           => 0
);
$tidy = new \Tidy;
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();

//this outputs tidied version 
echo (string)$tidy;

then this tidied version is dumped into ACE editor where user edits things and saves that html chunk. So when I save it -> I want it to be "un-tydied", meaning it shouldn't contain line breaks or tag indentation. How would I do that with tidy?

Was it helpful?

Solution

No reason to use a library to achieve this. Use preg replace to match \r or \n or both and replace it with nothing.

preg_replace( "/\r|\n/", "", $ACE);

As far as removing indentation is concerned, that's just white space, which can be removed with php's function trim

$trimmed = trim($ACE);

Further, trim accepts a 2nd argument which you can pass in and have trimmed as well:

$trimmed = trim($ACE, '\t.');

Which for instance will trim the tab character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top