2012-01-04 25 views
1

我有一個簡單的PHP函數輸出HTML。修改現有的PHP函數以返回字符串

<?php 
function get_header() { 
?> 
<div id="header"> 
    <div class="page-width"> 
    <!-- And a lot more HTML after this line. --> 
<?php 
} 
?> 

所以,當我打電話給get_header()時,函數輸出HTML。

什麼是最簡單的選項來調整此函數作爲字符串返回HTML?我是否需要爲此功能創建一個包裝?換句話說,我希望能夠做到var html_string = get_header_wrapper(),其中html_string包含上述所有HTML。

我能想到的一件事是複製函數並使其返回一個字符串。但是,這會非常低效,因爲它引入了大量的代碼重複。

<?php 
function get_header_wrapper() { 
    var ret = <<<EOD 
    <div id="header"> 
    <div class="page-width"> 
    <!-- And a lot more HTML after this line. --> 
    ... 
    EOD; 

    return ret; 
} 
?> 
+2

第二個代碼塊有什麼問題?(除了EOD之外;不應該在任何空格之前加上 – 2012-01-04 17:08:18

+0

)爲什麼你不能修改現有的函數? – 2012-01-04 17:08:48

+0

@Truth:第二塊代碼可以正常工作,但是我正在尋找一個解決方案,我不需要將這段HTML代碼複製到另一個函數中。對不起,我不清楚我沒有權限直接修改'get_header()'。謝謝。 – moey 2012-01-04 17:15:47

回答

8

您可以使用output bufferingDocs獲得該函數的輸出:

ob_start(); 
get_header(); 
$html = ob_get_clean(); 

如果您需要不止一次,你可以用它到它自己的功能:

/** 
* call a function and return it's output as string. 
* 
* @param callback $function 
* @param array $arguments (optional) 
* @param var $return (optional) the return value of the callback 
* @return string function output 
*/ 
function ob_get_call($function, array $arguments = array(), &$return = NULL) 
{ 
    ob_start(); 
    $return = call_user_func_array($function, $arguments); 
    $buffer = ob_get_clean(); 
    return $buffer; 
} 

用法:

$html = ob_get_call('get_header'); 

的答案是,今天流行的,這裏是另一個函數來獲得的包括輸出:

/** 
* include a file and return it's output as string. 
* 
* @param string $file 
* @param array $variables (optional) keys as variable names and values as variable values 
* @param var $includeReturn (optional) the return value of the include 
* @return string function output 
*/ 
function ob_get_include($file, array $variables = array(), &$includeReturn = NULL) 
{ 
    $includeFilename = $file; 
    unset($file); 
    extract($variables); 
    unset($variables); 
    ob_start(); 
    $includeReturn = include($includeFilename); 
    return ob_get_clean(); 
} 

用法:

include.php

<div class="greeting"> 
    Hello <em><?php echo htmlspecialchars($name); ?></em>! 
</div> 

使用:

$variables = array(
    'name' => 'Marianne', 
); 
$html = ob_get_include('include.php', $vars); 

相關:

+1

+1爲一個乾淨的輸出緩衝解決方案。 – 2012-01-04 17:09:50

+0

另外+1的非常詳細的幫助。謝謝! – moey 2012-01-04 17:24:23

1

使用輸出緩衝:

<?php 
function get_header() { 
    ob_start(); 
    <div id="header"> 
    <div class="page-width"> 
    <!-- And a lot more HTML after this line. --> 
    ... 
    $content = ob_get_contents(); 
    ob_end_clean(); 
    return $content; 
} 
?> 

你甚至可以做字符串進行一些處理返回之前。 OB岩石!

+0

使用輸出以這種方式緩衝是完全矯枉過正的,而且你正在修改原始功能。 – 2012-01-04 17:10:17

0

得到一個HTML內容功能的內容的最佳動作以下,然後它應該是你的手:

function html_content(){ 
ob_start();?> 
    <div class="some-div" id='the_id'>html text goes here 
    </div> 
<?php 
return ob_get_clean(); 
} 

在這種方法中,你可以輕鬆地複製粘貼所需的HTML標籤和內容到您的代碼並輕鬆使用它們。

我用過ob_start();在html標籤開始之前,並在結束我用返回ob_get_clean();