【问题标题】:How to change HTML title in PHP without breaking the XHTML markup validation?如何在不破坏 XHTML 标记验证的情况下更改 PHP 中的 HTML 标题?
【发布时间】:2010-12-30 18:55:34
【问题描述】:

这是网站的结构:

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-&gt;content 文件是一个动态的 PHP 文件,这意味着内容也会根据查询而变化。

例如,如果我有一个带有 ?img=1?img=2 之类查询的图像页面,我的 LoadParameter 类将只查看查询的 img 部分,并且知道页面的内容应该是image.phpimage.php 但是会再次查看查询并确定要加载的确切图像。

这给我带来了问题,因为我想为不同的图像使用不同的 &lt;title&gt;&lt;/title&gt;。所以我的解决方案就是在内容页面中设置&lt;title&gt;&lt;/title&gt; 元素。这可行,但它破坏了W3C 处的 XHTML 标记验证,因为它使网站的结构如下:

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

并且不允许在&lt;body&gt;&lt;/body&gt; 中包含&lt;title&gt;&lt;/title&gt;

那么如何在不破坏 XHTML 标记验证的情况下更改标题?

注意:我不能使用 javascript,因为这样搜索引擎将无法看到页面的标题。我需要直接在 PHP 中完成。

提前感谢。

【问题讨论】:

    标签: php validation title xhtml


    【解决方案1】:

    为什么不进行第二次包含以在适当的位置执行标题?

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

    【讨论】:

      【解决方案2】:

      你不能只是改变 PHP 代码,以便你可以做类似的事情:

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

      【讨论】:

      • 标题是在 $parameter->content 中确定的,所以在我包含 $parameter->content 之前无法打印标题
      • $paremeter->content 的内容是什么样的?所以我可以看看是否有另一种解决方案?在旁注中,我认为代码的设计不符合您的需求。如果我是你,我会在设计 PHP 代码之前首先考虑我想做什么。这可以防止在开发/维护/扩展/等的后期阶段出现很多麻烦。
      • 感谢您的建议。我实际上最终改变了结构,以便能够制作这个词。 $parameter->content 是一个 php 文件,其中包含内容的 HTML 代码。
      【解决方案3】:

      我会将所有 &lt;head&gt; 代码移动到一个名为 html_head($title) 之类的“通用函数”中,然后让它将标题放在它所属的位置。

      然后只需从页面中调用该函数即可修复。

      不要忘记在该函数中包含&lt;body&gt; 标签,否则它将不起作用!

      详细;)

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

      然后在$parameter->content中,调用html_head("Title")

      【讨论】:

        【解决方案4】:

        如果 $parameter->content 可以在不显示其 HTML 代码的情况下被包含,而是使用 $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>
        

        【讨论】:

          【解决方案5】:

          这就是我解决问题的方法。

          我将 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;
          

          这里是内容 PHP 文件的基本结构。由于唯一可能包含该文件的页面是我的设计页面,我可以在其中引用$parameter

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

          这不是一个非常干净的解决方案,但效果很好。

          【讨论】:

            猜你喜欢
            • 2010-12-21
            • 2014-07-29
            • 1970-01-01
            • 1970-01-01
            • 2011-01-07
            • 1970-01-01
            • 1970-01-01
            • 2019-02-28
            • 2011-10-05
            相关资源
            最近更新 更多