3

次のようなhtml構造を含む変数があります。

$txt = '<table>
           <tr>
               <td>
              </td>
           </tr>
       </table>';

変数内に次のステートメントを書きたい:

include_once('./folder/file.php');

次のように書き込もうとしましたが、失敗しました。

$txt = '<table>
               <tr>
                   <td>';
                   include_once('./folder/file.php');
                  $txt.='</td>
               </tr>
           </table>';

そして、私はそのように試してみましたが、うまくいきません:

$txt = '<table>
                   <tr>
                       <td>
                       {include_once('./folder/file.php');}
                     </td>
                   </tr>
               </table>';

どうやってやるの?申し訳ありませんが、php と html の混合の専門家ではないので、小さな助けをいただければ幸いです ??

4

3 に答える 3

7

出力バッファ関数を使用します。

ob_start();
include('./folder/file.php');
$include = ob_get_clean();

$txt = '<table>
           <tr>
               <td>' . $include . '</td>
           </tr>
       </table>';

http://php.net/obを参照

出力バッファは、ブラウザを空にするか、削除するか、終了するまで、ブラウザに送信するすべてのものを収集します。

http://www.php.net/manual/en/function.ob-get-clean.php

于 2013-10-01T12:14:02.617 に答える
0

次のようにする必要があります。連結を呼び出すと思います:

$table_content = include_once('./folder/file.php');
$txt = '<table>
               <tr>
                   <td>
                         ' . $table_content . '
                   </td>
               </tr>
         </table>';

また...

$txt = '<table>
               <tr>
                   <td>
                         ' . include_once('./folder/file.php') . '
                   </td>
               </tr>
         </table>';

簡単に言えば、テキストを出力してから変数を出力する必要がある場合、次のようにします。

$color = brown
$state = lazy

echo "The quick" . $color . "fox jumped over the" . $state . "dog";

これにより、次の結果が得られます。

The quick brown fox jumped over the lazy dog

詳細については、次を参照してください:文字列の連結

于 2013-10-01T12:14:58.303 に答える
0

これを試して

$txt = '<table>
           <tr>
               <td>';
$txt.=include_once('./folder/file.php');
$txt.='</td>
           </tr>
       </table>';
print_r($txt);   
于 2013-10-01T12:35:43.643 に答える