如何在不破坏XHTML标记验证的情况下在PHP中更改HTML标题?(How to change HTML title in PHP without breaking the XHTML markup

编程入门 行业动态 更新时间:2024-10-18 10:34:07
如何在不破坏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类只会查看查询的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中完成它。

提前完成。

Here is the structure of the web site:

PHP index file

//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 design file

Just the generic structure. Obviously it has a lot more design elements.

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

Question

So here is the problem I experience. The $parameter->content file is a dynamic PHP file, meaning the content also changes according to the query.

For instance if I have a image pages with queries like ?img=1 and ?img=2, my LoadParameter class will only look at the img part of the query and will know that the content of the page should be image.php. image.php however will look at the query again and figure out exactly what image to load.

This causes issues for me because I want to have a different <title></title> for different images. So my solution was just to set the <title></title> element in the content page. This works but it breaks the XHTML markup validation at W3C because it makes the structure of the site to be the following:

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

And having <title></title> within <body></body> is not allowed.

So how can I change the title without breaking the XHTML markup validation?

Note: I can't use javascript because then Search engines would not be able to see the title of the page. I need to do it directly in PHP.

Thanx in advance.

最满意答案

为什么不做第二次包括在适当的地方执行标题?

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

This is how I solved the issue.

I changed the PHP design to something like:

//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;

And here is the basic structure of the Content PHP file. Since the only page which can possibly include the file is the my design page, I can reference $parameter in it.

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

It's not a very clean solution but it works fine.

更多推荐

本文发布于:2023-08-07 13:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464480.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标记   情况下   标题   如何在   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!