I have HTML code like:
-
$html = preg_replace('~>\s+<~', '><', $html);
But I don't see the point of this. If you're trying to make the data size smaller, there are better options.
讨论(0)
-
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);
}
讨论(0)
-
//...
public function compressHtml($content)
{
$content = preg_replace('~>\s+<~', '><', $content);
$content = preg_replace('/\s\s+/', ' ', $content);
$i = 0;
while ($i < 5) {
$content = str_replace(' ', ' ', $content);
$i++;
}
return $content;
}
讨论(0)
- 热议问题