通过php从html源代码中删除空格

时间:2013-01-30 15:52:57

标签: php replace

<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>

我有以下这些HTML,我想删除额外的空格,需要结果如下:

<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>

任何人都可以借助PHP和preg,str_replace等帮助我解决这个问题

1 个答案:

答案 0 :(得分:0)

从标记开始后的文本开头删除空格[\n\t\r\ ]

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

从标记前面的文本末尾删除空格[\n\t\r\ ]

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