1

I was made small framework for fast form create in PHP, all work correct when I write code manually but I have problems when I want include code from txt file (I wand give this option for my user who will just write string in txt), check my example for better understand:

$my_test_variable = "How are you?";
$show_this = "|Example 1|Example 2|$my_test_variable|";
$a = explode ("|",$show_this);
echo "First example: $a[1] Second example: $a[2] Variable to show: $a[3]";
// This example output: First example: Example 1 Second example: Example 2 Variable to show: How are you?

Second example:

//test.txt -> |Example 1|Example 2|$my_test_variable|

$show_this = file_get_contents ("test.txt");
$a = explode ("|",$show_this);
echo "First example: $a[1] Second example: $a[2] Variable to show: $a[3]";
// This example output: First example: Example 1 Second example: Example 2 Variable to show: $my_test_variable
// This is problematic because I want that my $my_test_variable show "How are you";

Do you have idea how I can put $variable in txt file and when file_get_contents threat this like variable not just a string?

user3300811
  • 105
  • 1
  • 1
  • 7
  • 1
    This is something like [eval](http://uk.php.net/eval) does, but please don't use it. It's a huge security hole. The users can do whatever they want with your script. – machineaddict Mar 13 '14 at 14:09
  • I just found out [this answer](http://stackoverflow.com/a/283232/1057527), which is exactly what you need. – machineaddict Mar 13 '14 at 14:12

1 Answers1

3

You can try with:

$a = explode ("|",$show_this);
foreach ($a as &$val) {
  if (strpos($val, '$') === 0) {
    $val = ${substr($val, 1)};
  }
}

echo "First example: $a[1] Second example: $a[2] Variable to show: $a[3]";
hsz
  • 148,279
  • 62
  • 259
  • 315