PHP Str_替换为echo函数

PHP Str_替换为echo函数,php,function,echo,str-replace,Php,Function,Echo,Str Replace,下面的函数是写在插件“我是逆向工程”核心中的代码的一部分。问题是我需要对它进行stru替换,但我不能,因为它已经设置为echo 功能是 function similar_posts($args = '') { echo SimilarPosts::execute($args); } 我在我的页面中使用similor\u posts()调用它,但我在主题中真正需要做的是调用$related=similor\u posts(),不过该函数设置为echo。我该如何改变这一点 我试过这个 f

下面的函数是写在插件“我是逆向工程”核心中的代码的一部分。问题是我需要对它进行stru替换,但我不能,因为它已经设置为echo

功能是

function similar_posts($args = '') {
    echo SimilarPosts::execute($args);
}
我在我的页面中使用
similor\u posts()
调用它,但我在主题中真正需要做的是调用
$related=similor\u posts()
,不过该函数设置为echo。我该如何改变这一点

我试过这个

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

但这并没有产生任何结果。

将函数包装到使用它的另一个函数中

将函数包装到使用它而不是echo的另一个函数中

因此,您有:

 return SimilarPosts::execute($args);
而不是:

 echo SimilarPosts::execute($args);
使用,而不是回声

因此,您有:

 return SimilarPosts::execute($args);
而不是:

 echo SimilarPosts::execute($args);
做了

function get_similar_posts($args = '') {
    return SimilarPosts::execute($args);
}
并在
页面上获得类似的帖子()

我应该想到这一点。

做到了

function get_similar_posts($args = '') {
    return SimilarPosts::execute($args);
}
并在
页面上获得类似的帖子()


应该想到这一点。

从函数返回:

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

从函数返回

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

如果要使用值
SimilarPosts::execute($args)
returns,则需要在
get\u similar\u posts
中使用关键字“return”

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

如果您无法更改
get_similor_posts
的定义,则有多种方法可以抓取
similor_posts
打印的内容,即使它是“设置为echo”

这可以通过使用PHP中可用的

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'

如果要使用值
SimilarPosts::execute($args)
returns,则需要在
get\u similar\u posts
中使用关键字“return”

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

如果您无法更改
get_similor_posts
的定义,则有多种方法可以抓取
similor_posts
打印的内容,即使它是“设置为echo”

这可以通过使用PHP中可用的

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'

尝试
returnsimilarposts::execute($args)
,然后您可以执行
$related=similar\u posts()
尝试
返回SimilarPosts::execute($args)
,然后您可以执行
$related=similor_posts()
OP可以在设计代码时更改代码。所以我认为缓冲区控制是错误的。OP能够在他梦想工程它时更改代码。所以我认为缓冲区控制是错误的。return是一个关键字,而不是一个函数。请不要将
()
中返回的任何内容包装起来。如果不是这样,我觉得它总是错误的,我也看不出有任何理由不这样做。如果您(作为一个示例)希望返回一个变量我的引用,但将其包装在
()
中,那么将来很容易出错,PHP自己的手册建议不要通过引用返回,除非你必须这样做。现在,ObjExt总是通过引用传递,数组之类的东西都使用写时拷贝,所以通过引用返回几乎没有什么好处。PHP自己的手册也建议使用return而不使用
()
,return是一个关键字,而不是一个函数。请不要将
()
中返回的任何内容包装起来。如果不是这样,我觉得它总是错误的,我也看不出有任何理由不这样做。如果您(作为一个示例)希望返回一个变量我的引用,但将其包装在
()
中,那么将来很容易出错,PHP自己的手册建议不要通过引用返回,除非你必须这样做。现在,ObjExt总是通过引用传递,而数组之类的东西都使用写时拷贝,因此通过引用返回几乎没有什么好处。PHP自己的手册也建议使用不带
()
的返回,