簡體   English   中英

如何在不中斷XHTML標記驗證的情況下在PHP中更改HTML標題?

[英]How to change HTML title in PHP without breaking the XHTML markup validation?

這是網站的結構:

PHP索引文件

//my class for analyzing the PHP query
$parameter = new LoadParameters();

//what this does is it accesses the database
//and according to the query, figures out what should be
//loaded on the page
//some of the things it sets are:
//    $parameter->design -  PHP file which contains the design
//                          HTML code of the page
//    $parameter->content - Different PHP file which should be loaded
//                          inside the design file
$parameter->mysqlGetInfo($_SERVER['QUERY_STRING']);

//load the design file
include($parameter->design);

PHP設計文件

只是通用結構。 顯然,它具有更多的設計元素。

<html>
  <head>
     ...
  </head>
  <body>
    <?php
       //this loads the content into the design page
       include($parameter->content);
    ?>
  </body>
</html>

所以這是我遇到的問題。 $parameter->content文件是一個動態PHP文件,這意味着內容也會根據查詢而變化。

例如,如果我有一個圖像頁面,其中包含諸如?img=1?img=2 LoadParameter類的查詢,我的LoadParameter類將僅查看查詢的img部分,並且知道該頁面的內容應為image.php 但是image.php會再次查看查詢,並准確找出要加載的圖像。

這給我帶來了問題,因為我想為不同的圖像使用不同的<title></title> 因此,我的解決方案是在內容頁面中設置<title></title>元素。 此方法有效,但由於使網站的結構如下,因此破壞了W3C的XHTML標記驗證:

<html>
  <head>
     ...
  </head>
  <body>
    ...
    <title>sometitle</title>
    ...
  </body>
</html>

並且不允許在<body></body>中包含<title></title>

那么,如何在不破壞XHTML標記驗證的情況下更改標題?

注意 :我無法使用javascript,因為這樣搜索引擎將無法看到頁面標題。 我需要直接在PHP中進行操作。

提前感謝。

為什么不包括第二部分以在適當的位置顯示標題?

<html>
    <head>
     <?php
       inlcude($parameter->title);
     ?>
      ...
    </head>
    <body>
    <?php
    //this loads the content into the design page
       include($parameter->content);
    ?>
    </body>
</html>

您不能只是更改PHP代碼以便執行以下操作:

<html>
  <head>
     <title><? print($parameter->title); ?></title>
  </head>
  <body>
    <?php
       //this loads the content into the design page
       include($parameter->content);
    ?>
  </body>
</html>

我將所有<head>代碼移到名為html_head($ title)之類的“通用函數”中,然后將標題放在它所屬的位置。

然后,只需在頁面內調用該函數即可,該函數已修復。

不要忘記在該函數中包含<body>標記,否則它將無法正常工作!

詳細;)

function html_head($title) {?>
    <html>
        <head>
            <title><?=$title?></title>
            <!-- Put whatever you want... here! -->
        </head>
        <body>
<?}

然后在$ parameter-> content中,調用html_head(“ Title”)

如果可以在不顯示$ HTML代碼的情況下包含$ parameter-> content,而是使用$ parameter-> display(或類似的)功能顯示HTML代碼,則會更容易。 這樣,您可以在文件的開頭包含PHP代碼,而不必擔心無法訪問標題。

<?php
  require_once($parameter->content);
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="UTF-8" /> 
    <title><?php echo $parameter->title; ?></title>
  </head>
  <body>
    <?php
       echo $parameter->display;
    ?>
  </body>
</html>

這就是我解決問題的方式。

我將PHP設計更改為:

//get the content PHP file
//inside the file I set the following variables
//which are used below:
//$parameter->title - the string which contains the title
//$parameter->html - the string which contains the HTML content
include($parameter->content);

//string which will contain the html code of the whole page
$html = <<<EndHere
<html>
  <head>
      <title>
EndHere;
//add title
$html .= $parameter->title;
$html .= <<<EndHere
      </title>
  </head>
  <body>
EndHere;
//add the content of the page
$html .= $parameter->html;
$html .= <<<EndHere
  </body>
</html>
EndHere;

//output the html
echo $html;

這是Content PHP文件的基本結構。 由於唯一可能包含文件的頁面是我的設計頁面,因此可以在其中引用$parameter

//set the title
$parameter->title = "sometitle";

//set the content HTML
$parameter->html = "some HTML here";

這不是一個很干凈的解決方案,但是效果很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM