In the production of template content output, very often use placeholders such as <%=%>, the regular way to easily replace this content, work only touch Java, PHP, JS, three languages simple implementation: Java version Import Java.util.hashmap;import Java.util.map;import Java.util.regex.matcher;import Java.util.regex.Pattern; public class Testrex {public static void main (string[] args) {Map Map = new HashMap (); Map.put ("name", "Penngo"); Map.put ("date", "2013-01-17"); Pattern p = pattern.compile ("<%= (\\w+?) %> "); String str = "<%=DATE%>, Hello <%=name%>"; Matcher m = p.matcher (str); StringBuffer sb = new StringBuffer (); Boolean result = M.find (); while (result) {String key = M.group (1); String value = Map.get (key); M.appendreplacement (SB, value); result = M.find (); } m.appendtail (SB); System.out.println (Sb.tostring ()); }} PHP version ' Penngo ', ' date ' = ' 2013-01-17 '); function Replacestr ($key, $data) {return $data [$key];} $str = ' <%=date%>, Hello <%=name%> '; $str = Preg_replace ("/<%= (\w+?) %>/ise "," Replacestr (' \\1 ', \ $data) ", $str); Echo $str;? > JS Version Run result output: 2013-01-17, Hello Penngo
http://www.bkjia.com/PHPjc/477810.html www.bkjia.com true http://www.bkjia.com/PHPjc/477810.html techarticle in the production of template content output, very often use placeholders such as%=%, the regular way is easy to replace this content, work only touch Java, PHP, JS, three languages simple ...