0

I'm trying to run a find replace of multiple values to do a mail merge effect on an HTML signature stored in the database.

I can get the string part replaced no worries, but for some reason it's leaving the "[" & "]" behind in the output.

Merge tags in the HTML would look like this: [FirstName], [LastName]

Original HTML would look like this:

Kind regards

[FirstName] [LastName]

After running the mailmerge function it should look like this:

Kind regards

John Smith

Here is what I've come up with so far, and Im sure the issue is something small:

public function merge_user_signature() {
    $user = $this->get_user_by_id();
    //spit_out($user);

    $authorisedMergeTags = array (
        "[FirstName]" => $user->firstName, 
        "[LastName]" => $user->lastName
    );

    $keys = array_keys($authorisedMergeTags);
    $values = array_values($authorisedMergeTags);
    $html = $this->get_user_signature();

    $mergedSignature = preg_replace($keys, array_values($authorisedMergeTags), $html);

    return $mergedSignature;
}

Thanks in advance

2
  • Can't speak about that specific replace since I've not looked up the docs, however [ and ] are special characters in regex'es and should normally (if you want them to be used literally) be prefixed with backslash, as in \[. Commented Mar 19, 2016 at 0:57
  • First thing, you don't need a regex. Commented Mar 19, 2016 at 0:59

1 Answer 1

1

You don't need to use a regex to deal with literal strings (whatever the situation):

return strtr($html, $authorisedMergeTags);
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.