【问题标题】:Buffer more information with PHP buffer使用 PHP 缓冲区缓冲更多信息
【发布时间】:2015-04-18 08:28:28
【问题描述】:

我正在寻找缓冲更多文本的解决方案。我有以下(Set Page Title using PHP

<?
ob_start (); 
?>
<!DOCTYPE>

<head>
<title><!--TITLE--></title>
</head>

<body>
<?
$pageTitle = 'Title of Page'; // Call this in your pages' files to define the page title
?>
</body>
</html>
<?
$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
echo str_replace ('<!--TITLE-->', $pageTitle, $pageContents);
?>

它只适用于标题,但我想缓冲 4 条类似这样的信息:

<?
ob_start ();
?>
<!DOCTYPE>

<head>
    <meta property="og:title" content="<!--TITLE-->" />
    <meta property="og:url" content="<!--URL-->" />
    <meta property="og:image" content="<!--IMG-->" />
    <meta property="og:description" content="<!--DESCRIPTION-->" />
</head>
<body>
<?
    $pageTitle = 'Title of Page'; 
    $pageUrl = 'http://anything.com'; 
    $pageImg = 'http://anything.com/lol.png'; 
    $pageDesc = 'One sentence is here';
?>
</body>
</html>
<?
$pageContents = ob_get_contents (); 
ob_end_clean (); 
echo str_replace ('<!--DESCRIPTION-->', $pageDesc, $pageContents);
echo str_replace ('<!--IMG-->', $pageImg, $pageContents);
echo str_replace ('<!--URL-->', $pageUrl, $pageContents);
echo str_replace ('<!--TITLE-->', $pageTitle, $pageContents);
?>

【问题讨论】:

    标签: php meta


    【解决方案1】:

    您正在回显页面内容 4 次。不要那样做,而是将替换结果再次放入 pagecontent 变量中并在最后回显一次

    $pageContents= str_replace ('<!--DESCRIPTION-->', $pageDesc, $pageContents);
    $pageContents= str_replace ('<!--IMG-->', $pageImg, $pageContents);
    $pageContents= str_replace ('<!--URL-->', $pageUrl, $pageContents);
    $pageContents= str_replace ('<!--TITLE-->', $pageTitle, $pageContents);
    echo $pageContents;
    

    【讨论】:

      猜你喜欢
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      相关资源
      最近更新 更多