Php正则表达式替换
Php正则表达式替换
PHP替换
1function split_curly_options1($text)
2{
3 $retArr = array();
4
5 if (empty($text)) {
6 return $retArr;
7 }
8 $options = preg_split('|,|', $text);
9
10 foreach ($options as $opt) {
11 if (empty($opt)) { continue; }
12 if (preg_match('/(.*)=(.*)/', $opt, $match)) {
13 $retArr[$match[1]] = $match[2];
14 } else {
15 $retArr[$opt] = '';
16 }
17 }
18 return $retArr;
19}
20
21[[PageList {size=5,category=XX,titlelength=Z9,countname=XXcount} <div>%title%|%title%-%time%</div>]]
22
23// This macro transcludes another page into a wiki page.
24function view_macro_transclude($args)
25{
26 global $pagestore, $ParseEngine, $ParseObject, $HeadingOffset, $PgTbl;
27 static $visited_array = array();
28 static $visited_count = 0;
29
30 $previousHeadingOffset = $HeadingOffset; // Backup previous version
31
32 // Check for CurlyOptions, and split them
33
34 preg_match("/^(?:\s*{([^]]*)})?\s*(.*)$/", $args, $arg);
35
36 $options = split_curly_options1($arg[1]);
37 $search = $arg[2];
38
39 $q1 = $pagestore->dbh->query("SELECT t1.title, t1.version, t1.author, t1.time, " .
40 "t1.username, LENGTH(t1.body), t1.comment, " .
41 "t1.mutable, MAX(t2.version) " .
42 "FROM $PgTbl AS t1, $PgTbl AS t2 " .
43 "WHERE t1.title = t2.title " .
44 "GROUP BY t2.title, t1.version " .
45 "HAVING t1.version = MAX(t2.version)".
46 "limit 0," . $options[size]);
47
48 $text = '';
49
50 $patenArr = array('(%title%)',
51 '(%time%)'
52 );
53
54 while(($result = $pagestore->dbh->result($q1)))
55 {
56
57 $arr = array($result[0],$result[3]);
58
59 // $text = $text . preg_replace('(%([a-z]+)%)', "make_result('$1')", $search);
60 $text = $text . preg_replace($patenArr, $arr, $search);
61 }
62 return $text;
1function split_curly_options1($text)
2{
3 $retArr = array();
4
5 if (empty($text)) {
6 return $retArr;
7 }
8 $options = preg_split('|,|', $text);
9
10 foreach ($options as $opt) {
11 if (empty($opt)) { continue; }
12 if (preg_match('/(.*)=(.*)/', $opt, $match)) {
13 $retArr[$match[1]] = $match[2];
14 } else {
15 $retArr[$opt] = '';
16 }
17 }
18 return $retArr;
19}
20
21[[PageList {size=5,category=XX,titlelength=Z9,countname=XXcount} <div>%title%|%title%-%time%</div>]]
22
23// This macro transcludes another page into a wiki page.
24function view_macro_transclude($args)
25{
26 global $pagestore, $ParseEngine, $ParseObject, $HeadingOffset, $PgTbl;
27 static $visited_array = array();
28 static $visited_count = 0;
29
30 $previousHeadingOffset = $HeadingOffset; // Backup previous version
31
32 // Check for CurlyOptions, and split them
33
34 preg_match("/^(?:\s*{([^]]*)})?\s*(.*)$/", $args, $arg);
35
36 $options = split_curly_options1($arg[1]);
37 $search = $arg[2];
38
39 $q1 = $pagestore->dbh->query("SELECT t1.title, t1.version, t1.author, t1.time, " .
40 "t1.username, LENGTH(t1.body), t1.comment, " .
41 "t1.mutable, MAX(t2.version) " .
42 "FROM $PgTbl AS t1, $PgTbl AS t2 " .
43 "WHERE t1.title = t2.title " .
44 "GROUP BY t2.title, t1.version " .
45 "HAVING t1.version = MAX(t2.version)".
46 "limit 0," . $options[size]);
47
48 $text = '';
49
50 $patenArr = array('(%title%)',
51 '(%time%)'
52 );
53
54 while(($result = $pagestore->dbh->result($q1)))
55 {
56
57 $arr = array($result[0],$result[3]);
58
59 // $text = $text . preg_replace('(%([a-z]+)%)', "make_result('$1')", $search);
60 $text = $text . preg_replace($patenArr, $arr, $search);
61 }
62 return $text;
参照:
http://www.php.net/manual/zh/function.preg-replace.php