HTML을 변수에 저장해야 할 때 골때린다.
$html = '<textarea id="bbp_{$context}_content" class="{$editor_class}" name="bbp_{$context}_content" cols="60" rows="{$textarea_rows}" tabindex="{$tabindex}">{$post_content}</textarea>';
이딴 식이 돼 버리니 말이다. HTML 코드 어시스트 기능도 활용할 수 없어 답답하다.
그런데 아래처럼 쓰면 HTML 코드 어시스트 기능을 사용하면서, PHP 밖에서 HTML을 쓰고 그걸 변수에 저장할 수 있다.
<?php // Start an output buffor ob_start(); ?> 여기는 변수로 저장된다. <? // Put the output into a usable variable $output = ob_get_contents(); // Flush the output buffer ob_end_clean(); echo var_dump($output); // result : string '여기는 변수로 저장된다.' (length=25)
핵심은 ob_start()
와 $output = ob_get_contents()
, 그리고 ob_end_clean()
이다.