0

Is this possible?

What I am trying to accomplish:

  • Create a html template in the form of a string
  • Inside the string, add a script, something like include 'php/countries.php';
  • Echo entire string to html page

Everything but the 2nd step works. I would like to see a php file echo the question being asked, onto the html page, including an echo from another php file.

EXAMPLE

echo "<div id=\"first\"><?php include \'countries.php\'; ?></div>";

I have tried the above, as well as the below:

EXAMPLE

echo "<div id=\"first\">".include 'countries.php'."</div>";

Would this require eval?

Any and all help is appreciated.

5
  • The include function returns a boolean not a string. You should create a countries() function that returns the information you're looking for and just call the function accordingly. Commented Oct 9, 2014 at 18:50
  • 2
    echo "<div id=\"first\">"; include 'countries.php'; echo "</div>"; Commented Oct 9, 2014 at 18:51
  • @rosscowar I always mix up require and include.. thanks Commented Oct 9, 2014 at 18:55
  • @Mike So this works perfectly. However, is there any hidden issues to breaking up the echos like this? Commented Oct 9, 2014 at 18:57
  • @Ricky No, there isn't. It just goes from one statement to 3 separate statements. Commented Oct 9, 2014 at 18:58

3 Answers 3

2

Seems a bit silly, but you could do the following:

echo "<div id=\"first\">" . file_get_contents('countries.php') . "</div>";

Or...

echo "<div id=\"first\">";
include "countries.php";
echo "</div>";

Or...

$externalfile = compileexternal('countries.php');
function compileexternal($file) {
    ob_start();
    require $file;
    return ob_get_clean();
}

echo "<div id=\"first\">" . $externalfile . "</div>";

If none of these are what you need, please update the question. There are a dozen ways.

Sign up to request clarification or add additional context in comments.

5 Comments

That's probably not what is wanted considering countries.php is a PHP file and could contain server-side code.
I do like this method, although @Mike was right, countries.php DOES contain more code.
@Ricky See my comment in your question then.
Thanks for the third option. I've never seen this used. Will be looking into this!
Does ob_end_clean(); automatically happen once the scope of the function is closed?
0

You can use

eval()

But it is not a good practice.

2 Comments

Not to extend this conversation, but I actually do use eval in one particular case. It's better not to use it but if you need to, just separate it from user input.
@Mike you are probably confusing it with exec() or system(). php.net/manual/bg/function.eval.php bg2.php.net/manual/en/function.system.php eval parses a string just as an included/required filed gets parsed. It is not a good practice(unlike require/include) because a string variable can hold anything (deppends really, but you know)
0

You can use a regular expression.

For example, your string could be;

<div id="first">{{countries.php}}</div>

You'd then do;

$string = "<div id='first'>{{test2.php}}</div>";

echo preg_replace_callback("/(\{\{.+\}\})/", function($matches) {
   include_once( str_replace(array("{", "}"), "", $matches[0]));
}, $string);
  • Check the file exists if( file_exists() )
  • Check the file can be included (we don't want to include ../../../../../etc/passwd

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.