0

ファイル内にいくつかの文字列を保存してい*.propertiesます。文字列の例は次のとおりです。

sentFrom= {$oEmails->agentName}から {$oEmails->customerCount} 人にメールを送信しています。

私の関数はから値を取得しsendingFrom、その文字列をページに出力しますが、内部を自動的に解析しません{$oEmails->agentName}。手動で解析せずに、PHP で変数を文字列から本来あるべきものに変換する方法はありますか?

4

4 に答える 4

4

を変更できる場合*.propertiesは、簡単な解決策を次に示します。

# in file.properties
sendingFrom = Sending emails from %s, to %s people.

そして、sprintf%sを使用して正しい値に置き換えます。

// Get the sendingFrom value from file.properties to $sending_from, and:
$full_string = sprintf($sending_from, $oEmails->agentName, $oEmails->customerCount);

これにより、アプリのロジック (変数とその取得方法) をプレゼンテーション (実際の文字列スキーム、に格納されている) から分離できますfile.properties

于 2012-05-23T16:57:38.773 に答える
2

ただの代替案です。

$oEmails = new Emails('Me',4);
$str = 'sendingFrom=Sending emails from {$oEmails->agentName}, to {$oEmails->customerCount} people.';

// --------------

$arr = preg_split('~(\{.+?\})~',$str,-1,PREG_SPLIT_DELIM_CAPTURE);
for ($i = 1; $i < count($arr); $i+=2) {
    $arr[$i] = eval('return '.substr($arr[$i],1,-1).';');
}
$str = implode('',$arr);
echo $str;
// sendingFrom=Sending emails from Me, to 4 people.
于 2012-05-23T17:51:02.187 に答える
0

通常のすべてのセキュリティ キャビアで Eval を使用できます

何かのようなもの。

$string = getStringFromFile('sendingFrom');

$FilledIn = eval($string);
于 2012-05-23T16:55:40.640 に答える