PHP, JS, Java Regular replacement text way to make template content output _php Tutorial

Source: Internet
Author: User
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 = "&LT;%=DATE%&GT;, 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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.