PHP如何从文本区域读取[your_name]

时间:2016-06-02 06:24:11

标签: php mysql ajax xml mysqli

检查:

realmObject.setData(jsonObject.optString("SELECTOR"));

在上面的行中,我的php用户管理的文本区域中有[]的字段名称,用于电子邮件模板。 请告诉我PHP如何阅读,Dear [NAME], <br /><br /> Someone (hopefully you) requested a password reset at [SITE_URL]. <br /><br /> To reset your password, please follow the following link: [EMAIL_LINK] 这.... 是否有功能..如果是,它是什么以及如何...? 我希望你们都能理解我想知道的事情......

谢谢

最好的问候

3 个答案:

答案 0 :(得分:1)

您可以使用preg_match_all匹配方括号所包含的所有关键字:

var_dump

通过$matches array(2) { [0]=> array(3) { [0]=> string(6) "[NAME]" [1]=> string(10) "[SITE_URL]" [2]=> string(12) "[EMAIL_LINK]" } [1]=> array(3) { [0]=> string(4) "NAME" [1]=> string(8) "SITE_URL" [2]=> string(10) "EMAIL_LINK" } } 变量,您将获得:

$keywords = array(
    'NAME' => 'Mihai MATEI',
    'SITE_URL' => 'http://example.com/',
    'EMAIL_LINK' => '<a href="mailto:contact@example.com">contact@example.com</a>',
);

所以,我们假设您将要替换一系列关键字:

foreach ($matches[1] as $key => $keyword) {
    $text = str_replace($matches[0][$key], $keywords[$keyword], $text);
}

您可以将模板中的关键字替换为:

var_dump

现在,如果$text Dear Mihai MATEI, <br /><br /> Someone (hopefully you) requested a password reset at http://example.com/. <br /><br /> To reset your password, please follow the following link: <a href="mailto:contact@example.com">contact@example.com</a> 变量将获得:

.container {
  width: 200px;
  background: #E0E0E0;
  font-size: 0;
  font-family: sans-serif, serif;
  padding: 20px;
}

.column {
  color: #757575;
  font-size: 16px;
  display: inline-block;
  width: 50%;
  padding: 10px;
  box-sizing: border-box;
  vertical-align: middle;
}

.txt-left {
  text-align: left;
  font-size: 24px;
}

.txt-right {
  font-size: 12px;
  text-align: right;
}

答案 1 :(得分:0)

在php方面,只需替换[NAME],[SITE_URL],[EMAIL_LINK]

例如:

$message = $_POST['msg']; // this is you text area content
$message = str_replace('[NAME]', "My Name", $message);
$message = str_replace('[SITE_URL]', "www.xyz.com", $message);
$message = str_replace('[EMAIL_LINK]', "abc@gmail.com", $message);

答案 2 :(得分:0)

使用str_replace

$arrReplaceWords  = array('[NAME]', '[SITE_URL]', '[EMAIL_LINK]');
$arrReplaceValues = array('Your Name', 'Your Site Url', 'Your Email link');

$text = str_replace($arrReplaceWords, $arrReplaceValues, $text);