Regular text replacement method for php, js, and java to produce template content output_PHP tutorial

WBOY
Release: 2016-07-14 10:07:54
Original
1191 people have browsed it

When making template content output, placeholders such as <%=%> are often used. It is very convenient to replace this content with regular methods. In my work, I only come into contact with java, php, and 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
$data = array('name'=>'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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477810.htmlTechArticleWhen producing template content output, placeholders such as %=% are often used, and regular The method is very convenient to replace this content. At work, I only come into contact with java, php, and js. The three languages ​​are simple...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template