如何动态填充PHP页面中的标签?
问题描述:
My code looks like:
index.php
:
<html>....
<title>My page</title>
....
<?php
switch ($_GET['id']){
case "tips":
include("tips.php");
$title = "Tips";
...
How do I get the title varible to the html title
tag?
All pages pass through index.php
.
我的代码如下: p>
如何获取html 所有页面都通过 index.php 代码>: p>
&lt; html&gt; ....
&lt; title&gt;我的页面&lt; / title&gt;
....
&lt; ?php
switch($ _GET ['id']){
case“tips”:
include(“tips.php”);
$ title =“提示”;
...
code> pre>
title code>标签的标题变量 ? p>
index.php code>。 p>
div>
答
Do your PHP before your HTML output.
<?php
switch ($_GET["id"]) {
case "tips":
$title = "Tips";
$page = "tips.php";
}
?>
<html>
<head>
<title><?php print $title; ?></title>
</head>
<body>
<?php include($page); ?>
</body>
</html>
答
< title > <?php echo $title; ?> < /title >
答
If you cannot use what Ahmet KAKICI suggests, add a script tag which sets document.title
<html>....
<title>My page</title>
....
<?php
switch ($_GET['id']){
case "tips":
include("tips.php");
$title = "Tips";
...
?>
<script>top.document.title="<?php=$title?>"</script>
My PHP is rusty, just wanted to suggest document.title
答
You can also do this with output buffering and a little string replacement.
<?php ob_start('ob_process'); ?>
<html>....
<title>{{{TITLE}}}</title>
....
<?php
switch ($_GET['id']){
case "tips":
include("tips.php");
$title = "Tips";
break;
}
function ob_process($buffer) {
global $title;
$buffer = str_replace('{{{TITLE}}}', $title, $buffer);
return $buffer;
}