Hello All,
I am attempting to get the contents of a file, and replace any variables within it with the variable values. I'm sure it's something easy that I'm missing, but unfortunately, I can't seem to get it to work. Any help would be appreciated!

For instance, if I have a file named test.xml that has the following content:
$member_name goes to the store

...so in PHP, I have the following code:

$member_name = "John";
$string = file_get_contents("test.xml");
echo $string;

I would expect this to output something like this:
John goes to the store

It however outputs:
$member_name goes to the store

Any ideas? Thanks.

    pdxf;11001503 wrote:

    I would expect this to output something like this:
    John goes to the store

    Why would you expect PHP to search through all data that you echo/print in search of parts of the data that look like PHP variable names and replace that part of the data with the value of a PHP variable with that name?

    If you want to replace '$member_name' with the value of the variable $member_name, then it sounds like [man]str_replace/man would be what you're after.

      bradgrafelman;11001506 wrote:

      Why would you expect PHP to search through all data that you echo/print in search of parts of the data that look like PHP variable names and replace that part of the data with the value of a PHP variable with that name?

      Hello bradgrafelman,
      I guess because if I do something like:

      <?php
      $member_name = "John";	
      $string = "$member_name goes to the store";
      echo $string;
      ?>

      It does what I expect, it prints "John goes to the store" (ie. it searches through all of the data of the string and replaces the data with the php variable).

      I guess what I am not getting is that I expect file_get_contents to take whatever is in that file and output it as a string variable, which to me should be the same as defining the string variable as I display above. It should then replace the variables with their actual values.

      I'll play around with str_replace, but since I'm not entirely sure what variables I would need to replace, I don't know if that works...

      Thanks,
      pdxf

        pdxf;11001512 wrote:

        Hello bradgrafelman,
        I guess because if I do something like:

        <?php
        $member_name = "John";	
        $string = "$member_name goes to the store";
        echo $string;
        ?>

        It does what I expect, it prints "John goes to the store" (ie. it searches through all of the data of the string and replaces the data with the php variable).

        That last part in parentheses isn't quite correct. The variable interpolation is actually done before that data is stored as the value for $string.

        In other words, variable interpolation is not done on any data that is being echo()/print()'ed (it's done before that). Thus, using a function like [man]file_get_contents/man to read data from a file into memory means you'll manually need to parse that data and replace the variable placeholders with their actual values.

        pdxf;11001512 wrote:

        I'll play around with str_replace, but since I'm not entirely sure what variables I would need to replace, I don't know if that works...

        Why wouldn't you know which variables you need to replace? Don't you know which variables you have defined?

        If not, I suppose you could use something like [man]preg_replace/man and compose a regular expression pattern to search through the string data and replace what looks like a variable name with the value of that actual variable (if it exists).

          Thanks for your time on this, I really appreciate your help.

          bradgrafelman;11001513 wrote:

          In other words, variable interpolation is not done on any data that is being echo()/print()'ed (it's done before that).

          I guess I'm assuming that variable interpolation gets done when the variable is created (ie. in this step: $string = "$member_name goes to the store"; ), so that by the time its printed, and since $member_name is defined before $string, the variable interpolation is already done.

          bradgrafelman;11001513 wrote:

          Why wouldn't you know which variables you need to replace? Don't you know which variables you have defined?

          I know which variables I need to replace now for this use, but in the future I may want to introduce new variables and have those replaced as well, without me needing to edit this script.

          Given a file named test.xml which contains: '$member_name goes to the store',
          I'm don't quite understanding why:

          [I]file_get_contents("test.xml") != "$member_name goes to the store"[/I]

          -to me, they should be equal.

          Thanks again...

            Variable names are parsed into their actual values only inside literal strings in the actual source code (and only if those strings are specified using double quotes or the heredoc syntax; see Variable parsing in the PHP manual for more info).

            Using [man]file_get_contents/man means PHP reads the contents of the file directly into memory. The data did not come from a literal string specified in your source code using double quotes or the heredoc syntax, thus there was no variable parsing done.

            I'm not one to normally spoonfeed people code (only because I like to think that cheats people out of the learning process of figuring things out on their own), but it's hard to describe my previous suggestion of using [man]preg_replace/man without actually putting up some code. Thus, here's an example of going about it that way:

            // First, some sample data
            $data = '
            This string is delimited in \'single\' quotes, thus
            $variable parsing won\'t occur. In other words, you\'d
            see the same data as you would echo\'ing this string as
            if you had used file_get_contents() instead.
            
            However, I\'ll bet you $10,000 of $currency_type that
            the $func_name() function will change that$punctuation';
            
            // Here are the variables we want to replace
            $currency_type = 'Monopoly money';
            $func_name = 'preg_replace';
            $punctuation = '!!1!1!!!1!eleven!!';
            
            // And here's the regexp magic...
            $new_data = preg_replace(
                    '/\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/e',
                    'isset( ${"$1"} ) ? ${"$1"} : \'$0\'',
                    $data
                    );
            
            // To compare the difference, see the output of this below
            echo "\n--------------------- Before ---------------------\n$data\n";
            echo "\n--------------------- After ----------------------\n$new_data\n";

            and here's the output from those echo statements:

            --------------------- Before ---------------------
            
            This string is delimited in *single* quotes, thus
            $variable parsing won't occur. In other words, you'd
            see the same data as you would echo'ing this string as
            if you had used file_get_contents() instead.
            
            However, I'll bet you $10,000 of $currency_type that
            the $func_name() function will change that$punctuation
            
            --------------------- After ----------------------
            
            This string is delimited in *single* quotes, thus
            $variable parsing won't occur. In other words, you'd
            see the same data as you would echo'ing this string as
            if you had used file_get_contents() instead.
            
            However, I'll bet you $10,000 of Monopoly money that
            the preg_replace() function will change that!!1!1!!!1!eleven!!
            

              Genius! Thanks so much bradgrafelman, you just made my night! You have been most helpful!

                Write a Reply...