0

First, I'm pulling text from one XML file which is just a name and putting it in a $name variable.

Then I'm pulling some other text from a different XML file and this text contains a variable, let's say:

<list>
 <wishes>
  <id>001</id>
  <text>Happy birthday, '.$name.'</text>
 </wishes>
</list>

As you see, I already tried to format it in a way which should conc... conat... parse it, but it doesn't. Once I include it in something as simple as this:

foreach ($people->user as $person) {

   if ($person->date == date('d.m')) {

      $name = $person->name;

         foreach ($list->wishes as $wish) {

            $text = $wish->text;

      }

   echo 'Wishes for '.$name.'.';
   echo 'Wish example: '.$text.' something something';

}

The result will be:

Wishes for John.
Wish example: Happy birthday, '.$name.' something something

It will treat the variable as plain text instead of replacing it with the actual name of the person.

So how do I force it to replace the $name variable with the actual name if the variable is contained in an XML file? BTW, I also tried to include echo or print in various places - inside the XML file, by setting another variable that would print the content of the XML file and then set that variable as the one that's echoed in the end - no luck, it just keeps treating it as text.

1
  • 1
    would $wish->text = str_replace('$name',$person->name,$wish->text) be enough? I feel allowing arbitary variable replacement or even using eval could open up your code to explotation. Where does the original text come from anyway? Commented Jul 29, 2019 at 22:11

1 Answer 1

0

Write it like this, so it's well-formed XML:

<text>Happy birthday, <var name="name" /></text>

Then parse your XML file like this (PHP short-array form):

$memory = [
    'name' => 'Christopher Robin',
    //'other' => 'values',
    //'go' => 'here',
];

$parsed = preg_replace_callback(
    '#<var name="(.*?)"\\s*/>#',
    function ($matches) use ($memory) {
        if (array_key_exists($matches[1], $memory)) {
            return $memory[$matches[1]];
        }
        return ''; // A referred variable is not in $memory!
    }
);

This should get you going (I'm not sure, off the top of my head, whether it's $matches[1] or $matches[0] - you can print out $matches to check which it is).

Your XML is now:

<text>Happy birthday, Christopher Robin</text>

Or you could declare a stream XML parser to process every tag and output it appropriately. Here, you would only output text to stdout, while var would return with the same method as above. The result would thus be the text "Happy birthday, Christopher Robin".

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

3 Comments

I'm not sure that PO is able to change the syntax of the variable to what you propose.
@Michael He did write "As you see, I already tried to format it", so I assumed he had control. By changing the regexp to #'\.\$(.*?)\.'# however, the answer should work with the syntax as it is now.
@LSemi cool - just wanted to point it out in case you'd missed it. :)

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.