[英]Replacing string with a function that outputing data already
我正在嘗試用已經顯示數據的函數替換字符串,或者您已經可以回顯數據
function getData(){
//echoing data from database, loop
echo 'printing output';
}
function output(){
return getData();
}
這是字符串
$str = "This is some text with php code <?php output();?>";
我試過這樣的 str_replace
str_replace("<?php output();?>", output(), $str);
問題是當我str_replace
用函數替換代碼時它已經顯示數據。 我還嘗試了其他函數,如preg_replace()
和preg_replace_callback()
輸出函數必須從 getData 返回內容:
function getData(){
//echoing data from database, loop
echo 'printing output';
}
function output(){
// Start buffer
ob_start();
// Call the function and store its contents on buffer
getData();
// Get buffer, clean buffer and return contents
return ob_get_clean();
}
// The string
$str = "This is some text with php code <?php output();?>";
// Replace and output
echo str_replace("<?php output();?>", output(), $str);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.