I have HTML code like:
-
As gpupo's post provided the cleanest solution for many different types of spacing formatting's. However, a minor but important piece was forgotten at the end! A final string trim :-p
Below is a tested and working solution.
function compress_html($content)
{
$i = 0;
$content = preg_replace('~>\s+<~', '><', $content);
$content = preg_replace('/\s\s+/', ' ', $content);
while ($i < 5)
{
$content = str_replace(' ', ' ', $content);
$i++;
}
return trim($content);
}
- 热议问题