Assign PHP script execution output variable?

I created a website, I probably didn’t do it as I should, but at that time I was new to PHP. Therefore, to save a lot of disappointment in trying to rewrite the script to display photos on my site, I need to run the * .php file and make a conclusion if it goes into var called "$ html". I know this may seem strange, but that is what I need.

From inside index.php, I include photos.php; In photos.php, I need to declare $ html with the output of a script called photos_page.php;

For instance: $html = parse_my_script("../photos_page.php");

thank

+5
source share
9 answers

: PHP / . , script :

:

: ob_start() ob_get_clean() is_readable()

function getScriptOutput($path, $print = FALSE)
{
    ob_start();

    if( is_readable($path) && $path )
    {
        include $path;
    }
    else
    {
        return FALSE;
    }

    if( $print == FALSE )
        return ob_get_clean();
    else
        echo ob_get_clean();
}

:

$path = '../photos_page.php';
$html = getScriptOutput($path);

if( $html === FALSE)
{
    # Action when fails
}
else
{
    echo $html;
}
+10

file_get_contents

$html = file_get_contents("http://www.yourwebsite.com/pages/photos_page.php");

//this will not work since it won't run through web server
//$html = file_get_contents("../photos_page.php");
+2

:

ob_start();
require('../photos_page.php');
$html = ob_get_contents();
ob_end_clean();
+2

file_get_contents("http://yourdomain.com/path/to/photos_page.php").


: , :

photos_page.php

<?php

function get_photos_html() {
    $html = // generate html
    return $html;
}

?>

main_file.php

<?php

include('../photos_page.php');

$html = get_photos_html();

?>
+1

. , , , :

ob_start();
include '../photos_page.php';
$html = ob_get_contents();
ob_end_clean();

, , , :

function parse_my_script($path)
{
    ob_start();
    include $path;
    $html = ob_get_contents();
    ob_end_clean();
    return $html;
}

, , , .

:

http://www.php.net/manual/en/ref.outcontrol.php

+1

.

, ob_start(). , ob_get_clean(), .

ob_start();
include "../photos_page.php";
$html = ob_get_clean();
+1

, .

, , . , , , .

0

ob_start() ob_flush(), ob_get_contents() .. http://us.php.net/manual/en/ref.outcontrol.php

PHP .

, , :)

, :

ob_start();
include('yourfile.php');
$html = ob_get_contents();
ob_end_clean();
0
source

If your photos_page.phpworks as follows:

<?php
    // very basic code
    $img = "/path/to/my/image.jpg";
    echo '<img src="' . $img . '">' . PHP_EOL;
?>

Then you can use:

$html = file_get_contents('http://mysite.com/photos_page.php');

But in fact, you should rewrite the code so you don't have to do it.

0
source

All Articles