1

I have an associative array, and a text with variables names inside, surrounded with brackets like this :

$message = "The value of var is: {var} ...-";

My array is like this:

$array=array('var'=>$var);

(Obviously this array can be bigger, and I don't know the name of the variables inside of it). Here is what I'm trying to do: I want to replace the {var} in my message by it's value in my array, like this:

$newMessage = preg_replace('#\{(.+)\}#',$array["$1"],$message);

Anyone knows how to do it?

Thank you very much!

1
  • 2
    Take a look at: preg_replace_callback() Commented Dec 29, 2015 at 11:35

1 Answer 1

3

@Rizier123's comment helped me, here's the code I used to solve my problem:

$newMessage=preg_replace_callback('#\{(.+)\}#',function($match)use($array){
    return $array[$match[1]];
},$message);
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.