Buffer more information with PHP buffer

我是研究僧i 提交于 2019-12-12 04:48:32

问题


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

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

回答1:


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;


来源:https://stackoverflow.com/questions/29714815/buffer-more-information-with-php-buffer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!