![](/img/trans.png)
[英]How to remove unnecessary whitespaces from string, so there are no extra spaces in HTML
[英]How to remove whitespaces from generated HTML?
的functions.php
<?php
function global_header($page)
{
echo "
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>" . $page . "</title>
<meta name='description' content='BTI320 Assignment 2' />
</head>
<body>
";
}
?>
<?php
function global_footer()
{
echo "
</body>
</html>
";
}
?>
當我在chrome / FF中查看我的頁面源時,我得到以下來源:
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Add</title>
<meta name='description' content='BTI320 Assignment 2' />
</head>
<body>
</body>
</html>
它縮進了大約3個標簽。 是否有PHP條帶功能或可以正確對齊的東西? 我不喜歡我的整個頁面HTML搞砸了。
我的預期輸出是不縮進的。
考慮使用模板引擎。 直接輸出HTML字符串被認為是不好的做法。
如果您不想使用第三方模板引擎,那么無論如何您都可以從這樣的簡化模板中受益:
page.tpl
模板文件 :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8' />
<title>{{title}}</title>
</head>
<body>
{{body}}
</body>
</html>
PHP:
// Loading HTML code that does not contain any undesired whitespace.
$code = file_get_contents('page.tpl');
// Replacing template variables with their values.
$code = str_replace(
array(
'{{title}}',
'{{body}}'
),
array(
'Example title',
'Page body'
),
$code
);
// Outputting resulting HTML code.
echo $code;
你得到縮進輸出的原因是你正在回應它們......只需從echo語句中刪除它們就可以去掉它們
<?php
function global_header($page)
{
echo "
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>" . $page . "</title>
<meta name='description' content='BTI320 Assignment 2' />
</head>
<body>";
}
?>
<?php
function global_footer()
{
echo "
</body>
</html>";
}
?>
這使得你的php更難以跟隨fut輸出將按照你的要求
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.