Bir ve sonuçları PHP bir String dahil olsun?

3 Cevap php

Dosya dnm.php bu suna benziyor:

<?php
echo 'Hello world.';
?>

Ben böyle bir şey yapmak istiyorum:

$test = include('test.php');

echo $test;

// Hello world.

Herkes doğru yolu beni işaret edebilir?

Edit:

Benim asıl hedefi bir veritabanından HTML ile iç içe PHP kod çekin ve onu işlemek için oldu. İşte ben yapıyorum sona ne:

// Go through all of the code, execute it, and incorporate the results into the content
while(preg_match('/<\?php(.*?)\?>/ims', $content->content, $phpCodeMatches) != 0) {
    // Start an output buffer and capture the results of the PHP code
    ob_start();
    eval($phpCodeMatches[1]);
    $output = ob_get_clean();

    // Incorporate the results into the content
    $content->content = str_replace($phpCodeMatches[0], $output, $content->content);
}

3 Cevap

Tampon kullanımı en iyi bahistir.


ob_start();
include 'test.php';
$output = ob_get_clean();

PS: Eğer yuva çıkış kalpleri içerik tamponlar de eğer gerekli olduğunu unutmayın.

Ayrıca dahil dosya çıktı dönmek yerine, onu baskı olabilir. Sonra ikinci örnekte yapmak gibi, bir değişken içine yakalayabilir.

<?php
    return 'Hello world.';
?>

dnm.php

<?php

return 'Hello World';

?>


<?php

$t = include('dnm.php');

echo $t;

?>

Sürece dahil dosya bir return deyimi vardır gibi çalışır.