简体   繁体   English

用HTML文本区域中的php变量替换单词

[英]Replacing words with php variables in a HTML text area

I have. 我有。 Few forms using general HTML. 使用一般HTML的表格很少。

One form sends emails to contacts in a database. 一种形式是将电子邮件发送给数据库中的联系人。

How can i make it so when I use something like ##!CONTACT_FORENAME!## it will replace it with the contacts forename from the database (forename column in contacts table) 当我使用##!CONTACT_FORENAME!##之类的方法时,它将如何用数据库中的联系人姓名代替(联系人表中的“姓名”列)

you have template 你有模板

$string_template = '
      Hello [username],
      .....
 ';

then you simple replace characters, 然后您只需替换字符,

$message = strtr($template, array('[username]' => $user_name_from_dtb));

我不知道我是否正确理解了您的问题,但是我会做这样的事情:

 <textarea><?php if (isset($var)) echo $var; ?></textarea>
$body='some text with [variable]';
$regex = "/\[.+?\]/"; // if not use brakets, change this.
if(preg_match_all($regex, $body, $matches, PREG_PATTERN_ORDER)) {
    foreach ($matches[0] as $key => $match) {
        $mvar=substr($match,1,strlen($match)-2); // get the variable name
        $value='this is a real value'; // get the variable value from db;
        $body=preg_replace("#\[".$mvar."\]#s",$value,$body);// replace 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM