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?

2
  • 1
    This is something like eval does, but please don't use it. It's a huge security hole. The users can do whatever they want with your script. Commented Mar 13, 2014 at 14:09
  • I just found out this answer, which is exactly what you need. Commented Mar 13, 2014 at 14:12

1 Answer 1

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]";
Sign up to request clarification or add additional context in comments.

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.