【问题标题】:PHP metatags description and keywords dynamicsPHP元标签描述和关键字动态
【发布时间】:2014-09-26 12:19:14
【问题描述】:

我希望动态编写关键字和描述元标记。我看到了这个thead,但我不能对元标记做同样的事情。
这是我的代码:http://runnable.com/VCSYsvEy0xov2fNi/my-for-php

index.php

<?php
ob_start (); // Buffer output
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<title><!--TITLE--></title>
    <meta http-equiv="Content-Language" content="it" />
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />  
    <meta name="keywords" content="<!--KEYWORDS-->" />
    <link rel="stylesheet" type="text/css" href="template/style.css">
    <script type="text/javascript" src="config/expand.js"></script>
    <script type="text/javascript" src="config/jquery-1.5.2.min.js"></script>
    <script type="text/javascript" src="config/scriptbreaker-multiple-accordion-1.js"></script>
    <script language="JavaScript">
        $(document).ready(function() {
            $(".topnav").accordion({
            accordion:false,
            speed: 500,
            closedSign: '[+]',
            openedSign: '[-]'
            });
        });
    </script>
</head>
<body>
        <div id="container">
            <div id="banner"><?php include('template/banner.php') ?></div>
            <!--<div id="nav_main"><?php include('template/nav_main.php') ?></div> -->
            <div id="navigation_left"><?php include("template/link_left.php") ?></div>      
            <div id="navigation_right" align="left"><?php include("template/link_right.php") ?></div>                               
            <div id="content"><?php include("$page.php") ?> </div>
            <div id="footer"><?php include('template/footer.php') ?></div>
        </div>
</body>
</html>
<?php
$pageContents = ob_get_contents (); // Get all the page's HTML into a string
$pageKeywords = ob_get_contents();
ob_end_clean (); // Wipe the buffer

// Replace <!--TITLE--> with $pageTitle variable contents, and print the HTML
echo str_replace ('<!--TITLE-->', $pageTitle, $pageContents);
echo str_replace ('<!--KEYWORDS-->', $pageKey, $pageKeywords);

?>

home.php(主页)

<?php $pageTitle = 'Full Metal Panic Italy | Home';?>
<?php $pageKey = 'full, metal, panic, fmp, fumoffu, the second raid, spoiler, mithril, amalgam, sigma';?>
<p style="background:#aaf">
  Lorem ipsum dolor sit amet, consectetaur adipisicing elit, sed do eiusmod tempor
  incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud 
  exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute 
  irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
  pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui 
  officia deserunt mollit anim id est laborum.
</p>

标题有效,但元无效。
我究竟做错了什么?

【问题讨论】:

    标签: php


    【解决方案1】:

    这样做

    $pageContents = ob_get_contents (); // Get all the page's HTML into a string
    ob_end_clean (); // Wipe the buffer
    
    // Replace <!--TITLE--> with $pageTitle variable contents, and print the HTML
    $pageContents = str_replace ('<!--TITLE-->', $pageTitle, $pageContents);
    $pageContents = str_replace ('<!--DESCRIPTION-->', $pageDesc, $pageContents);
    echo str_replace ('<!--KEYWORDS-->', $pageKey, $pageContents);
    

    【讨论】:

    • 完美!有用!我尝试对描述做同样的事情,但它复制了 html 并且不添加描述字符串。 echo str_replace ('', $pageDescription, $pageContents);
    • 不要复制正在打印数据的echo 行,如果您查看页面,您实际上将拥有两套完整的数据...滚动到底部。在继续之前,您需要对 PHP 课程进行一些基本介绍。为了解决这个重复上面的行并做描述部分。
    • 我已经复制并添加了 是的,我有 2 组数据
    • 我已经更新了我的答案,你可以看看如何实现。
    • 谢谢!能解释一下为什么要重复数据吗?
    猜你喜欢
    • 2017-05-01
    • 2014-11-21
    • 1970-01-01
    • 2015-11-26
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多