替换电子邮件模板PHP的字符串

时间:2019-02-27 09:30:31

标签: php preg-replace-callback

我有一个字符串$subject = "$$service$$ Appointment Approved",所以我需要用我的var替换$$ service $$,我该怎么办?我有这个...

public function __construct(Appointment $appointment) {
    $this->appointment = $appointment;
}

public function sendMail() {
    $mailSubject = $this->replaceString($subject);
}

public function replaceString($string) {
    $string = preg_replace_callback('/\$\$(.+?)\$\$/is', function($matches){
        $placeholder = $matches[1];
        if (property_exists($this->appointment, $placeholder)) {
            return $this->appointment->$placeholder;
        }
        if ($this->appointment->get($placeholder) !== null) {
            return $this->appointment->get($placeholder);
        }
    }, $string);

    return $string;
}

我编辑了函数:

public function replaceString($string) {
    $string = preg_replace_callback('/\$\$(.+?)\$\$/is', function($matches) {
        $placeholder = $matches[1];

        return $this->appointment->$placeholder;

    }, $string);

    return $string;
}

并且邮件正确到达,但是我具有DateTime属性,在电子邮件模板中,我具有$$ start $$并返回错误,因为DateTime类的对象无法转换为字符串。我怎么知道哪个变量将成为对象?

0 个答案:

没有答案