用PHP缓冲区缓冲更多信息 [英] Buffer more information with PHP buffer

查看:97
本文介绍了用PHP缓冲区缓冲更多信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种缓冲更多文本的解决方案.我有以下内容(使用PHP设置页面标题)

I looking for the sollution buffering more texts. I have the following (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条信息:

and it works only with title but i would like to buffer 4 information someting like this:

<?
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);
?>

推荐答案

您正在回显页面内容4次.不要这样做,而是将替换结果再次放在pagecontent变量中,并在末尾回显一次.

you are echo'ing page content 4 times. don't do that , instead put the replace result in pagecontent variable again and echo it once in the end

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

这篇关于用PHP缓冲区缓冲更多信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆