사이트 내 전체검색
PHP
[php] [Function] ob_start() , ob_get_contents() , ob_get_flush(), ob_flush(), ob_end_clean()
로빈아빠
https://cmd.kr/php/543 URL이 복사되었습니다.

본문

[Function] ob_start() , ob_get_contents() , ob_get_flush(), ob_flush(), ob_end_clean()

자주는 안쓰지만 많은 데이터를 처리 할때(실행시간이 길때) 간혹 쓰이는 함수 입니다.

우선 ob_start()로 버퍼링을 시작하고  ob_flush()으로 버퍼를 출력 하시면 됩니다.
버퍼 값을 반환 할려면 ob_get_contents()를 쓰시면 됩니다.

ob_start() - 출력 버퍼링을 켭니다
ob_get_contents() - 출력 버퍼의 내용을 반환
ob_get_contents() - 출력 버퍼 내용의 길이를 반환
ob_get_clean() - 현재 버퍼 내용을 얻고 현재 출력 버퍼를 삭제
ob_get_flush() - 출력 버퍼의 내용을 반환하고 출력 버퍼링을 종료
ob_clean() - 출력 버퍼의 모든 내용을 삭제
ob_flush() - 출력 버퍼를 전송
ob_end_clean() - 출력 버퍼를 지우고 출력 버퍼링을 종료
ob_end_flush() - 출력 버퍼를 전송하고 출력 버퍼링을 종료

예)
<?php
function callback($buffer)
{
  // 모든 apples를 oranges로 치환합니다.
  return (str_replace("apples", "oranges", $buffer));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php
ob_end_flush();
?>


예)
<?php
$cacheFile = 'cache.html';

if ( (file_exists($cacheFile)) && ((fileatime($cacheFile) + 600) > time()) )
{
    $content = file_get_contents($cacheFile);
    echo $content;
} else 
{
    ob_start();
    // write content
    echo '<h1>Hello world to cache</h1>';
    $content = ob_get_contents();
    ob_end_clean();
    file_put_contents($cacheFile,$content);
    echo $content;
}
?>


예)
<?php
ob_start();
echo "Hello ";
$out1 = ob_get_contents();
echo "World";
$out2 = ob_get_contents();
ob_end_clean();
var_dump($out1, $out2);
?>

댓글목록

등록된 댓글이 없습니다.

PHP
871 (9/18P)

Search

Copyright © Cmd 명령어 3.19.60.65