ob_start メソッドを使用した PHP のバッファ出力データ
    
    
            Kevin Amayi
    2023年1月30日
    
    PHP
    PHP Array
    
- 
          
            ob_startメソッドを使用して単純な文字列をバッファリングし、PHP のob_get_contentsメソッドを使用してデータを取得する
- 
          
            PHP で ob_startメソッドを使用して HTML データをバッファリングし、ob_get_contentsメソッドを使用してデータを取得する
- 
          
            ob_startメソッドとコールバック関数を使用して、文字列データをバッファリングし、文字列内の文字を置き換える
 
ob_start メソッドを使用してバッファを初期化し、自動的にバッファリングされる単純な文字列を出力します。次に、ob_get_contents メソッドを使用してバッファからデータを取得し、それを出力します。
また、バッファ ob_start メソッドを初期化してから、自動的にバッファリングされる HTML ブロックを出力します。次に、ob_get_contents メソッドを使用してバッファからデータを取得し、それを出力します。
最後に、バッファ ob_start メソッドを初期化し、自動的にバッファリングされる単純な文字列を宣言してから、ob_start メソッドに渡されたコールバックを使用して文字列内のデータを置き換えます。
ob_start メソッドを使用して単純な文字列をバッファリングし、PHP の ob_get_contents メソッドを使用してデータを取得する
ob_start を設定してから、自動的にバッファリングされた単純な文字列を出力します。次に、ob_get_contents を使用してバッファからデータを取得し、それを出力します。
<?php
ob_start();
echo("Hello there!"); //would normally get printed to the screen/output to browser
$output = ob_get_contents();
echo $output;
?>
出力:
Hello there! Hello there! 
PHP で ob_start メソッドを使用して HTML データをバッファリングし、ob_get_contents メソッドを使用してデータを取得する
ob_start を設定してから、自動的にバッファリングされた HTML データを出力します。次に、バッファリングされたデータを出力します。
<?php
ob_start();
?>
<div>
    <span>text</span>
    <a href="#">link</a>
</div>
<?php
$content = ob_get_contents();
?>
出力:
<div>
 <span>text</span>
 <a href="#">link</a>
</div>
ob_start メソッドとコールバック関数を使用して、文字列データをバッファリングし、文字列内の文字を置き換える
ob_start を設定してから、自動的にバッファリングされた HTML データを出力します。次に、バッファリングされたデータを出力します。
<?php
    //Declare a string variable
    $str = "I like PHP programming. ";
    echo "The original string: $str";
    //Define the callback function
    function callback($buffer)
    {
    //Replace the word 'PHP' with 'Python'
    return (str_replace("PHP", "Python", $buffer));
    }
    echo "The replaced string: ";
    //call the ob_start() function with callback function
    ob_start("callback");
    echo $str;
?>
出力:
The original string: I like PHP programming. The replaced string: I like Python programming. 
        チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe