如何动态填充PHP页面中的<title>标签?</title>

时间:2009-07-20 17:31:50

标签: php html

我的代码如下:

index.php

<html>....  
    <title>My page</title>

....

<?php
    switch ($_GET['id']){  
        case "tips":  
            include("tips.php");  
            $title = "Tips";
...

如何将标题变量设为html title标记?

所有网页都会通过index.php

4 个答案:

答案 0 :(得分:10)

在HTML输出之前执行PHP。

<?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>

答案 1 :(得分:2)

您也可以使用输出缓冲和一些字符串替换来完成此操作。

<?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;
}

答案 2 :(得分:0)

< title > <?php echo $title; ?> < /title >

答案 3 :(得分:0)

如果您无法使用Ahmet KAKICI建议的内容,请添加设置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>

我的PHP很生疏,只是想建议document.title