/**
* 将数组非0-9a-zA-Z!*() 替换为asicc 16进制
*
* @param array $params array
* @return array array
*/
private static function replaceParams($params)
{
foreach($params as $key=>$val)
{
$params[$key] = preg_replace_callback('/[^0-9a-zA-Z!*()]/',create_function('$matches','return "%".strtoupper(dechex(ord($matches[0])));'),$val);
}
return $params;
}
代码如上所示。
/// <summary>
/// 将数组非0-9a-zA-Z!*() 替换为asicc 16进制
/// </summary>
/// <param name="_params"></param>
/// <returns></returns>
public static Dictionary<String, String> ReplaceParams(Dictionary<String, String> _params)
{
Dictionary<String, String> new_params = new Dictionary<string, string>();
foreach(KeyValuePair<String, String> kvp in _params)
{
String key = kvp.Key;
String val = kvp.Value;
//$params[$key] = preg_replace_callback('/[^0-9a-zA-Z!*()]/',create_function('$matches','return "%".strtoupper(dechex(ord($matches[0])));'),$val);
val = Regex.Replace(val, "[^0-9a-zA-Z!*()]", new MatchEvaluator(Replace));
new_params.Add(key, val);
}
return new_params;
}
/// <summary>
/// 正则匹配替换
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
private static string Replace(Match m)
{
int c = Convert.ToChar(m.Value);
string s16 = Convert.ToString(c, 16);
return "%" + s16.ToUpper();
}
自己解出来了。。。