-4
<ul class="c2">
    <li><a href="hire-project-manager/">Hire Project Manager </a></li>
    <li><a href="hire-ios-developer/"> Hire iPhone Developer </a></li>
    <li><a href="hire-ios-developer/">  Hire iPad Developer </a></li>
    <li><a href="hire-android-developer/">Hire Android Developer </a></li>
    </li>
    <li><a href="hire-3d-artist/">Hire 3D Artist</a></li>
</ul>
</div>
<h1>Get In Touch With Us</h1>
<div class="copyright-footer">Copyright &copy; All rights reserved  </div>

I have following of these html and i want to remove extra spaces and need result something like:

<ul class="c2">
    <li><a href="hire-project-manager/">Hire Project Manager</a></li>
    <li><a href="hire-ios-developer/">Hire iPhone Developer</a></li>
    <li><a href="hire-ios-developer/">Hire iPad Developer</a></li>
    <li><a href="hire-android-developer/">Hire Android Developer</a></li>
    </li>
    <li><a href="hire-3d-artist/">Hire 3D Artist</a></li>
</ul>
</div>
<h1>Get In Touch With Us</h1>
<div class="copyright-footer">Copyright &copy; All rights reserved</div>

Can anyone help me to solve this with the help of PHP and preg, str_replace etc

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169

1 Answers1

0

Remove whitespace [\n\t\r\ ] from the start of text after tag start:

$html=preg_replace('/>\s+/','>',$html);

Remove whitespace [\n\t\r\ ] from the end of text before a tag:

$html=preg_replace('/\s+</','<',$html);
CSᵠ
  • 10,049
  • 9
  • 41
  • 64
  • That type of question will always end one way: http://stackoverflow.com/a/1732454/1064325 – falsarella Jan 30 '13 at 16:11
  • It's just about whitespace @falsarella, regex is great for this. Of course you could use a built in parser to disassemble the html, trim the text and patch it back together but that would be an overkill just for this. – CSᵠ Jan 30 '13 at 16:18
  • And if you have something like this `' > < '`? Inspecting your answer, you can see a live sample. But, probably your answer will fit to the OP's needs! – falsarella Jan 30 '13 at 16:26