1

Suppose I have the HTML code as below

<table width="600" border="0" cellpadding="0" cellspacing="0" align="center" bgcolor="#ffffff">
    <tr>
                    <td><img src="/images/pho_01.jpg" width="600" height="63" border="none" style="border: 0px;" alt="photo" /></td>

                    <td></td>


        <td></td>
                </tr>
            </table>

The HTML code is not readable, I want to convert to something likes

<table width="600" border="0" cellpadding="0" cellspacing="0" align="center" bgcolor="#ffffff">
<tr>
<td><img src="/images/pho_01.jpg" width="600" height="63" border="none" style="border: 0px;" alt="photo" /></td>
<td></td>
<td></td>
</tr>
</table>

I have ever used the $.trim(), but not works, could someone please suggest a way to
1. remove space(but don't remove the space between attribute likes table width="600"...)
2. remove empty line

Thanks

1
  • 1
    Easy way to do it is open the html file in a text editor like gedit or notepad++ and hold down shift+tab to indent backwards. If you need a script, I would search for tags and remove the spaces before it on the same line. Commented Jul 30, 2011 at 7:54

3 Answers 3

4

Assuming your html is stored in a javascript string, you'll have to split it up into multiple lines and use trim on each line.

e.g.

function multilineTrim(htmlString) {
   // split the string into an array by line separator
   // call $.trim on each line
   // filter out the empty lines
   // join the array of lines back into a string
   return htmlString.split("\n").map($.trim).filter(function(line) { return line != "" }).join("\n");
}
Sign up to request clarification or add additional context in comments.

Comments

1

try http://jsbeautifier.org/ It works with HTML too

Comments

0

Try this http://www.textfixer.com/tools/remove-line-breaks.php

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.