时间:2021-07-01 10:21:17 帮助过:14人阅读
输出模式替换结果
< ?php
//字符串
$string = "Name: {Name}< br>\nEmail:
{Email}< br>\nAddress: {Address}< br>\n";
//模式
$patterns =array(
"/{Address}/",
"/{Name}/",
"/{Email}/"
);
//替换字串
$replacements = array (
"No.5, Wilson St., New York, U.S.A",
"Thomas Ching",
"tom@emailaddress.com",
);
//
public static String PregReplace(this String input, string[] pattern, string[] replacements) {
if (replacements.Length != pattern.Length) throw new ArgumentException("Replacement and Pattern Arrays must be balanced");
for (var i = 0; i < pattern.Length; i++)
{ input = Regex.Replace(input, pattern[i], replacements[i]); }
return input;
}