preg_match_all()を使用してオカレンスを検索し、この配列を使用してデータベース内の値を検索し、その後str_replaceを使用して各値を置き換えることができます。
まず、すべてのオカレンスをフェッチする必要があります。
preg_match_all('/{(.*?)}/', $string, $matches, PREG_PATTERN_ORDER);
一致配列は次のようになります。
Array
(
[0] => Array
(
[0] => {}
[1] => {25}
[2] => {FIELD_CONTRACTTEXT}
)
[1] => Array
(
[0] =>
[1] => 25
[2] => FIELD_CONTRACTTEXT
)
)
次に、配列を循環して、置き換える必要のある個々の要素を見つけることができます。
以下を使用してデータベースをチェックし、値を置き換えることができます。
mysql_connect("localhost", "root", "");
mysql_select_db("data");
$string = "Enter Contarct Fields wherever you want and enclose it with {}.You can use field's id or field's fieldcode for example {25} or {FIELD_CONTRACTTEXT}";
preg_match_all('/{(.*?)}/', $string, $matches, PREG_PATTERN_ORDER);
foreach($matches[1] as $match) {
$sql = mysql_query("SELECT * FROM table WHERE `element`='".addslashes($match)."'");
if(mysql_num_rows($sql)==1){
$row = mysql_fetch_assoc($sql);
str_replace($string, "{".$match."}", $row['value']);
}
}
echo $string; //echos final output with replaced values