3

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

1
  • No, I don't want to save file with some functions. I create the html file manually. The php code only for reading and displaying contents in html files Commented Sep 25, 2016 at 15:13

5 Answers 5

4

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

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

1 Comment

It's fine. But I have so many many variables in my func. I defined all these variables at the code before. And I must rewrite all of them in the str_replace function ? I really want it's auto detect the exist variable without replacing.
1

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);
}

Comments

1

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'));

Comments

0

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?

1 Comment

It didn't work. I think because the function "file_get_contents" not pass the variables. But if I use "include" instead, it didn't return value for my variable $str
0

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) ;

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.