Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-包含header.PHP后如何更改页面的元内容?_Php_Output Buffering - Fatal编程技术网

PHP-包含header.PHP后如何更改页面的元内容?

PHP-包含header.PHP后如何更改页面的元内容?,php,output-buffering,Php,Output Buffering,Page.php <?ob_start(): include('header.php'); ?> <? $pageTitle='Page'; echo 'body html'; ?> <? $pg=ob_get_contents(); ob_end_clean(); echo str_replace('<!--TITLE-->',$pageTitle,$pg); ?> header.php <!-- Basic --> <

Page.php

<?ob_start():
include('header.php');
?>

<? $pageTitle='Page';
echo 'body html';
?>

<?
$pg=ob_get_contents();
ob_end_clean();
echo str_replace('<!--TITLE-->',$pageTitle,$pg);
?>

header.php

<!-- Basic -->
<title><!--TITLE--> | DEMO</title>
<!-- Page Description and Author -->
<meta name="description" content="">
<meta name="author" content="">

|演示

在页面顶部插入header.php文件后,如何在my page.php中包含元描述内容和作者内容以及其他元名称

因此,我知道您必须使用包含html内容的php文件,并且您希望将“header.php”的元“description”和“author”复制到“page.php”

通过php获取html内容是不可能的。您可以在变量中保护元“description”和“author”,并在“header.php”中写入元,然后将这些变量放到“page.php”中

header.php:

<html>
<head>
<title>DEMO</title>
<?php
$description = '...';
$author = 'me';
echo '<meta name="description" content="'.$description.'">';
echo '<meta name="author" content="'.$author.'">';

function getDescription(){
    return $description;
}
function getAuthor(){
    return $author;
}
?>
</head>
<body>...</body>
</html>

演示
...
page.php:

<html>
<head>
<title>DEMO</title>
<?php
$description = getDescription();
$author = getAuthor();
echo '<meta name="description" content="'.$description.'">';
echo '<meta name="author" content="'.$author.'">';
?>
</head>
<body>...</body>
</html>

演示
...

因此我知道您必须使用包含html内容的php文件,并且您希望将“header.php”的元“description”和“author”复制到“page.php”

通过php获取html内容是不可能的。您可以在变量中保护元“description”和“author”,并在“header.php”中写入元,然后将这些变量放到“page.php”中

header.php:

<html>
<head>
<title>DEMO</title>
<?php
$description = '...';
$author = 'me';
echo '<meta name="description" content="'.$description.'">';
echo '<meta name="author" content="'.$author.'">';

function getDescription(){
    return $description;
}
function getAuthor(){
    return $author;
}
?>
</head>
<body>...</body>
</html>

演示
...
page.php:

<html>
<head>
<title>DEMO</title>
<?php
$description = getDescription();
$author = getAuthor();
echo '<meta name="description" content="'.$description.'">';
echo '<meta name="author" content="'.$author.'">';
?>
</head>
<body>...</body>
</html>

演示
...

与其使用某种字符串操作在某个地方插入文本,不如先构建应用程序,让业务逻辑运行,然后再运行模板代码。在写任何东西之前,先弄清楚你想要的标题和其他元数据是什么

与其使用某种字符串操作在某个地方插入文本,不如先构建应用程序,让业务逻辑运行,然后再运行模板代码。在写任何东西之前,先弄清楚你想要的标题和其他元数据是什么

如果您不想修改头文件。。您可以执行以下操作。。。但我建议您让代码结构比这更好:(它需要更动态

<?php

ob_start();
include('header.php');
$pageTitle='Page';
echo 'body html';
$pg=ob_get_contents();


$description = 'your description';
$author = 'your author';



//delete old desc and author
$pg = str_replace('content=""', '', $pg);
//add anew desc and author
$pg = str_replace('name="description"', 'name="description" content="'.$description.'" ', $pg);
$pg = str_replace('name="author"', 'name="author" content="'.$author.'" ', $pg);

ob_end_clean();
echo str_replace('<!--TITLE-->',$pageTitle,$pg);


?>

如果您不喜欢修改头文件..您可以执行以下操作..但我建议您改进代码结构:(它需要更动态

<?php

ob_start();
include('header.php');
$pageTitle='Page';
echo 'body html';
$pg=ob_get_contents();


$description = 'your description';
$author = 'your author';



//delete old desc and author
$pg = str_replace('content=""', '', $pg);
//add anew desc and author
$pg = str_replace('name="description"', 'name="description" content="'.$description.'" ', $pg);
$pg = str_replace('name="author"', 'name="author" content="'.$author.'" ', $pg);

ob_end_clean();
echo str_replace('<!--TITLE-->',$pageTitle,$pg);


?>

您可以在ob#u开始调用中定义回调函数,该调用在ob#u开始网页的示例#1中有记录

function modify_header($buffer)
{
  // do your changes in $buffer here
}

ob_start(modify_header);

无论如何,我建议使用一些模板框架来完成所有此类工作

您可以在ob#u start调用中定义回调函数,如ob#u start网页中的示例1所示

function modify_header($buffer)
{
  // do your changes in $buffer here
}

ob_start(modify_header);

无论如何,我建议使用一些模板框架来完成所有这类工作

您是否可以修改header.php文件?您是否可以修改header.php文件?您是否可以修改header.php文件?感谢您的输入,工作了..尽管仍在测试动态更新的可扩展性感谢输入,工作了..尽管仍在测试对于动态更新的可扩展性输入的hanks,已工作..尽管仍在测试动态更新的可扩展性