Phong Hoang
Phong Hoang

Reputation: 173

PHP file_get_contents with variables

I have a function:

myfunc () {
   $a = "John";
   $str = "Hello $a, how are you today?"
   return $str;
}

echo myfunc(); //Hello John, how are you today?

I want to save all the sentenses in that function to another file, because their length is too long to put it in my function.

myfunc () {
  $a = "John";
  $str = file_get_contents("inc_file.html");
  return $str;
}

inc_file.html:

Hello $a, how are you today?

But it didn't return as I expected. The variable $a is not changed to John. Please help me ! Thanks

Upvotes: 3

Views: 6363

Answers (5)

Claytronicon
Claytronicon

Reputation: 1456

here's a method that replaces any matching variables from the loaded file string. It's a modified version of code from the link below. I modified it to check for string type. It assumes the variables are global.

How can I output this dynamic data without eval?

function interpolate( $string ){
foreach ($GLOBALS as $name => $value){
    // avoid array to string and object conversion errors
  if(is_string($value)){
    $string = str_replace( '$'.$name, $value, $string );
  }
}
$string = preg_replace( '/[$]\\w+/', '', $string );
return $string;
}
$emailTemplate = file_get_contents('inc_file.html');
$emailTemplate = interpolate($emailTemplate);
echo $emailTemplate;

I found this after searching for a way to do it without eval() like shown below:

$emailTemplate = file_get_contents('inc_file.html');
$emailTemplate = htmlentities($emailTemplate);
eval("\$emailTemplate = \"$emailTemplate\";");
echo html_entity_decode($emailTemplate) ;

Upvotes: 0

Rasclatt
Rasclatt

Reputation: 12505

What you have is fine, you can just use a preg_replace_callback() or in this case str_replace() to do what you want. Just do some adjustments:

HTML:

"Hello ~val~, how are you today?"

PHP

function myfunc ($a)
    {
        $str = str_replace('~val~',$a,file_get_contents("inc_file.html"));
        return $str;
    }

echo myfunc('John');

Simple preg_replace_callback() example:

function myfunc ($string,$a)
    {

        $str = preg_replace_callback('/(~[^~]{1,}~)/',function($v) use (&$a)
            {
                return array_shift($a);
            },$string);

        return $str;
    }

$string = "hello my name is ~name~. I come from ~city~";
echo myfunc($string,array('John','San Francisco'));

Upvotes: 1

Steve
Steve

Reputation: 1963

All that file_get_contents() does is return the contents of the file, as the function name suggests. It does not parse the contents which is what happens with a string in "s.

Using str_replace() on the returned file contents seems to achieve what you want.

$str = file_get_contents("inc_file.html");
$str = str_replace('$a', 'John', $str);
return $str;

If you wanted to replace multiple 'variables' you can pass arrays to str_replace, for example

$search = [$a, $b];
$replace = ['John', 'Brian'];

$str = file_get_contents("inc_file.html");
$str = str_replace($search, $replace, $str);
return $str;

See http://php.net/manual/en/function.str-replace.php

Upvotes: 4

u_mulder
u_mulder

Reputation: 54831

Line $str = file_get_contents("inc_file.html"); just gets file contents. It doesn't evaluate this contents and doesn't replaces vars with its' values.

Suppose, what would happen if file will be huge of there will be some specific text about php variables?

They all should be replaced with some values? I suppose not. So, you just have a string with symbols like $a.

And if you have to replace something in a string - use str_replace, simple code is:

function myfunc () {
    $a = "John";
    $str = file_get_contents("inc_file.html");
    return str_replace('$a', $a, $str);
}

Upvotes: 1

Tony Markov
Tony Markov

Reputation: 55

You can to include .php file but wit .html isn`t works because you include html not php code. If you make code and your file like this:

<?php
function myfunc() 
{
  $a = "John";
  $str = file_get_contents("inc_file.php");
  return $str;
}
?>

inc_file.php:

Hello <?php echo $a; ?>, how are you today?

Upvotes: 0

Related Questions