PHP - 如何在包含 header.php 之后更改页面的标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13009227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - how to change title of the page AFTER including header.php?
提问by John Stockton
page.php:
页面.php:
<?php
include("header.php");
$title = "TITLE";
?>
header.php:
头文件.php:
<title><?php echo $title; ?></title>
i want my title to be set AFTER including the header file. Is it possible to do this? I've seen some similiar stuff on php-fusion tvs: http://docs.php-fusion.it/temi:output_handling
我希望在包含头文件之后设置我的标题。是否有可能做到这一点?我在 php-fusion 电视上看到了一些类似的东西:http://docs.php-fusion.it/temi: output_handling
回答by we.mamat
expanding on Dainis Abols answer, and your question on output handling,
扩展 Dainis Abols 的答案,以及您关于输出处理的问题,
consider the following:
考虑以下:
your header.php has the title tag set to <title>%TITLE%</title>;
the "%" are important since hardly anyone types %TITLE% so u can use that for str_replace() later.
您的 header.php 的标题标签设置为<title>%TITLE%</title>; “%”很重要,因为几乎没有人输入 %TITLE%,所以你可以稍后将它用于 str_replace()。
then, you can use output bufferlike so
然后,您可以像这样使用输出缓冲区
<?php
ob_start();
include("header.php");
$buffer=ob_get_contents();
ob_end_clean();
$buffer=str_replace("%TITLE%","NEW TITLE",$buffer);
echo $buffer;
?>
and that should do it.
那应该这样做。
EDIT
编辑
I believe Guy's ideaworks better since it gives you a default if you need it, IE:
我相信盖伊的想法效果更好,因为如果您需要它,它会为您提供默认值,IE:
- The title is now
<title>Backup Title</title> - Code is now:
- 标题是现在
<title>Backup Title</title> - 代码现在是:
<?php
ob_start();
include("header.php");
$buffer=ob_get_contents();
ob_end_clean();
$title = "page title";
$buffer = preg_replace('/(<title>)(.*?)(<\/title>)/i', '' . $title . '', $buffer);
echo $buffer;
?>
回答by J Perera
1. Simply add $title variable before require function
1. 只需在 require 函数前添加 $title 变量
<?php
$title = "Your title goes here";
require("header.php");
?>
2. Add following code into header.php
2.在header.php中加入如下代码
<title><?php echo $title; ?></title>
回答by Peon
What you can do is, you store the output in a variable like:
您可以做的是,将输出存储在一个变量中,例如:
header.php
头文件
<?php
$output = '<html><title>%TITLE%</title><body>';
?>
PS: You need to remove allechos/prints etc so that all possible output is stored in the $outputvariable.
PS:您需要删除所有回声/打印等,以便所有可能的输出都存储在$output变量中。
This can be easely done, by defining $output = '';at the start of the file and then find/replaceechoto $output .=.
这可以轻松完成,方法是$output = '';在文件开头定义,然后查找/替换echo为$output .=.
And then replace the %TITLE%to what you need:
然后将其替换%TITLE%为您需要的:
<?php
include("header.php");
$title = "TITLE";
$output = str_replace('%TITLE%', $title, $output);
echo $output;
?>
Another way is using javascriptin your code, instead of:
另一种方法是javascript在您的代码中使用,而不是:
<title><?php echo $title; ?></title>
Put this in there:
把这个放在那里:
<script type="text/javascript">
document.title = "<?=$title;?>"
</script>
Or jQuery, if you prefer:
或者 jQuery,如果你喜欢:
<script type="text/javascript">
$(document).ready(function() {
$(this).attr("title", "<?=$title;?>");
});
</script>
回答by Guy
Expanding a little on we.mamat's answer, you could use a preg_replaceinstead of the simple replace and remove the need for a %title%altogether. Something like this:
稍微扩展一下 we.mamat 的答案,您可以使用preg_replace而不是简单的替换,并且完全不需要%title%。像这样的东西:
<?php
ob_start();
include("header.php");
$buffer=ob_get_contents();
ob_end_clean();
$title = "page title";
$buffer = preg_replace('/(<title>)(.*?)(<\/title>)/i', '' . $title . '', $buffer);
echo $buffer;
?>
回答by Nirav Ranpara
you can set using JavaScript
您可以使用 JavaScript 进行设置
<script language="javascript">
document.title = "The new title goes here.";
</script>
回答by Thilanka De Silva
Add this code on top your page
将此代码添加到页面顶部
<?php
$title="This is the new page title";
?>
Add this code on your Template header file (include)
将此代码添加到您的模板头文件(包括)
<title><?php echo $title; ?></title>
回答by JamilHammash
It's very easy. Put this code in header.php
这很容易。将此代码放在 header.php 中
<?
$sitename = 'Your Site Name'
$pagetitle;
if(isset($pagetitle)){
echo "<title>$pagetitle." | ". $sitename</title>";
}
else {
echo "<title>$sitename</title>";
}
?>
Then in the page put there :
然后在页面中放置:
<?
$pagetitle = 'Sign up'
include "header.php";
?>
So if you are on Index.php , The title is Your Site Name. And for example if you are on sign up page , The title is Sign up | Your Site Name
因此,如果您在 Index.php 上,则标题是您的站点名称。例如,如果您在注册页面,标题是注册 | 您的站点名称
回答by JamilHammash
Every Simple just using a function , I created it .
每个 Simple 只是使用一个函数,我创建了它。
<?
function change_meta_tags($title,$description,$keywords){
// This function made by Jamil Hammash
$output = ob_get_contents();
if ( ob_get_length() > 0) { ob_end_clean(); }
$patterns = array("/<title>(.*?)<\/title>/","<meta name='description' content='(.*)'>","<meta name='keywords' content='(.*)'>");
$replacements = array("<title>$title</title>","meta name='description' content='$description'","meta name='keywords' content='$keywords'");
$output = preg_replace($patterns, $replacements,$output);
echo $output;
}
?>
First of all you must create function.php file and put this function inside ,then make require under the MetaTags in Header.php . To use this function change_meta_tags("NEW TITLE","NEW DESCRIPTION",NEW KEYWORDS); . Don't use this function in Header.php !! just with another pages .
首先你必须创建 function.php 文件并将这个函数放在里面,然后在 Header.php 的 MetaTags 下 make require。使用这个函数 change_meta_tags("NEW TITLE","NEW DESCRIPTION",NEW KEYWORDS); . 不要在 Header.php 中使用这个函数!!只是与另一页。
回答by Mohammadreza
Use a jQueryfunction like this:
使用这样的jQuery函数:
$("title").html('your title');
$("title").html('your title');

