2011-12-14 5 views
1

다음 함수는 리버스 엔지니어링중인 플러그인의 핵심 부분에 작성된 코드의 일부입니다. 문제는 내가 str_replace를 수행 할 필요가 있고 이미 에코로 설정 되었기 때문에 할 수 없다는 것입니다.echo 함수의 PHP str_replace

기능은입니다.

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

답변

2

사용 return를 사용합니다. 그래서

당신이 가지고 : 대신

return SimilarPosts::execute($args); 

:

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

나는 그것을했지만 어쨌든 당신의 대답을 제외 할 것입니다. 감사합니다 –

+0

당신은 오신 것을 환영합니다. –

1

랩 또 다른 내부 기능을하는 당신은 에코 대신 output buffering.

+0

영업 이익은 코드를 변경 할 수 있습니다 그는 공학에 대한 환상을 품고 있습니다. SO 버퍼 제어는 제가 생각하기에는 잘못된 방식입니다. –

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)); 
} 
+1

return은 키워드가 아니라 함수입니다. '()'에 반환되는 것을 포장하지 마십시오. –

+0

그렇지 않을 때는 언제나 저에게 잘못 보입니다. 왜 안되는지 전혀 알 수 없습니다. – GordonM

+0

미래의 오류가 발생하기 쉽습니다. 예를 들어 변수를 내 참조로 되돌리고 싶지만'()'로 래핑하면 ** 작동하지 않습니다 **. –

2

당신이 값을 SimilarPosts::execute ($args) 수익률을 사용하려는 경우, 당신은 당신의 get_similar_posts 내부의 키워드 '수익'을 사용해야합니다.


당신이 "에코 설정"비록 similar_posts에 의해 인쇄 된 내용을 날치기하는 방법이 있습니다 get_similar_posts의 정의를 변경할 수없는 경우

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

.

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'