从html(php)文件中删除换行符

时间:2012-10-29 10:27:03

标签: php html split line break

我已经搜索过这个问题,我想从html中删除换行符,但在html文件中有一些拆分的php行,例如:

<html>
<head>
<title><?='abc'?></title>
</head>
<?php
if($_SESSION['test'] == 'yes'){
echo 'hello';
}
?>
123456
43567
<?='13245tryt57u68'?>
</body>
</html>

如何从此php文件中删除换行符?

2 个答案:

答案 0 :(得分:2)

您可以使用ob_start();

<?php ob_start();
//Start page output
?>
<html>
<head>
<title><?='abc';?></title>
</head>
<body>
<?php
if(isset($_SESSION['test']) && $_SESSION['test'] == 'yes'){
    echo 'hello';
}
?>
123456
43567
<?='13245tryt57u68';?>
</body>
</html>
<?php 
//End page output and assign the contents to a variable
$buffer = ob_get_contents();
ob_end_clean();

//Replace the new line with null
echo str_replace("\n",null,$buffer);
?>

<强>结果:

<html><head><title>abc</title></head><body>1234564356713245tryt57u68</body></html>

答案 1 :(得分:0)

如果您想在IDE中使用它: 使用替换功能,并用无字符替换\ n

如果您想为客户提供: 您必须配置htaccess以在将文件发送到客户端时删除换行符/缩进,即使用mod_pagespeed扩展名: http://www.the-art-of-web.com/system/mod-pagespeed-settings/和collapse_whitespace选项