2011-12-14 76 views
1

以下函數是寫入插件的核心代碼的一部分,我是逆向工程。它的問題是我需要對其執行str_replace,而我不能,因爲它已經設置爲echo。PHP Str_replace for echo函數

功能是。

function similar_posts($args = '') { 
    echo SimilarPosts::execute($args); 
} 

我把它用在我的similar_posts()頁面,但我真的需要我的主題做的是電話$related = similar_posts(),但該功能被設置爲呼應。我該如何改變這一點。

我試過了。

function get_similar_posts($args = '') { 
     SimilarPosts::execute($args); 
    } 

但是沒有產生任何結果。

+0

嘗試`返回SimilarPosts ::執行($參數);`,那麼你可以做的`$相關= similar_posts()` – codeling 2011-12-14 14:03:11

回答

2

使用return而不是回聲。

,讓您有:

return SimilarPosts::execute($args); 

代替:

echo SimilarPosts::execute($args); 
+0

我做到了,但除了你的答案,無論如何。謝謝 – 2011-12-14 14:03:19

+0

不客氣。 – 2011-12-14 14:05:25

1

包裹內的另一個功能在您使用output buffering.

+0

的OP是能夠改變的代碼因爲他是遐思工程。 SO緩衝控制是我認爲的錯誤方式。 – 2011-12-14 14:09:05

1

幹過..

function get_similar_posts($args = '') { 
    return SimilarPosts::execute($args); 
} 

,並在頁面上get_similar_posts();

應該想到的那。

1

return從功能:

function get_similar_posts($args = '') { 
    return SimilarPosts::execute($args); 
} 
3
function get_similar_posts($args = '') { 
    return (SimilarPosts::execute($args)); 
} 
2

如果你想使用的值SimilarPosts::execute ($args)回報,你需要使用關鍵字「回報」你get_similar_posts內。

function get_similar_posts ($args = '') { 
    return SimilarPosts::execute($args); 
} 

如果無法改變get_similar_posts定義有辦法搶奪通過similar_posts即使它的「設置爲呼應」打印的內容。

這可以通過使用PHP中的Output Control Functions完成。

function echo_hello_world() { 
    echo "hello world"; 
} 

$printed_data = ""; 

ob_start(); 
{ 
    echo_hello_world(); 

    $printed_data = ob_get_contents(); 
} 
ob_end_clean(); 

echo "echo_hello_world() printed '$printed_data'\n"; 

輸出

echo_hello_world() printed 'hello world'