I have HTML code like:
-
Thank you for posting this question. The problem is indeed dealing with whitespace bugs in certain environments. While the regex solution works in the general case, for a quick hack remove leading whitespace and add tags to the end of each line. PHP removes the newline following a closing ?>. E.g.:
<ul><?php ?>
<li><a id="nav-questions" href="/questions">Questions</a></li><?php ?>
<li><a id="nav-tags" href="/tags">Tags</a></li><?php ?>
<li><a id="nav-users" href="/users">Users</a></li><?php ?>
<li><a id="nav-badges" href="/badges">Badges</a></li><?php ?>
<li><a id="nav-unanswered" href="/unanswered">Unanswered</a></li><?php ?>
</ul>
Obviously this is sub-optimal for a variety of reasons, but it'll work for a localized problem without affecting the entire tool chain.
讨论(0)
-
Use regular expressions, like:
>(\s).*?<
讨论(0)
-
if you got 8 bit ASCII, is will remove them and keep the chars in range 128-255
$text = preg_replace('/[\x00-\x1F\xFF]/', " ", $text );
If you have a UTF-8 encoded string is will do the work
$text = preg_replace('/[\x00-\x1F\x7F]/u', '', $text);
for more information
you have this link
more information
讨论(0)
-
<?php
define(COMPRESSOR, 1);
function remove_html_comments($content = '') {
return preg_replace('/<!--(.|\s)*?-->/', '', $content);
}
function sanitize_output($buffer) {
$search = array(
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s' // shorten multiple whitespace sequences
);
$replace = array(
'>',
'<',
'\\1'
);
$buffer = preg_replace($search, $replace, $buffer);
return remove_html_comments($buffer);
}
if(COMPRESSOR){ ob_start("sanitize_output"); }
?>
<html>
<head>
<!-- comment -->
<title>Example 1</title>
</head>
<body>
<p>This is example</p>
</body>
</html>
RESULT: <html><head><title>Example 1</title></head><body><p>This is example</p></body></html>
讨论(0)
-
I can't delete this answer but it's no longer relevant, the web landscape has changed so much in 8 years that this has become useless.
讨论(0)
-
A RegEx replace could do the trick, something like:
$result = preg_replace('!\s+!smi', ' ', $content);
讨论(0)
- 热议问题