1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
| <?php
$reg = "#[aby\}]#"; $str = 'a\bc[]{}'; preg_match_all($reg, $str, $m); var_dump($m);
/* array(1) { [0]=> array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "}" } } */
// 顺序肯定环视 (?=exp) $reg = "#\b\w+(?=ing\b)#"; $str = "I'm singing while you're dancing."; preg_match_all($reg, $str, $m); var_dump($m);
/* array(1) { [0]=> array(2) { [0]=> string(4) "sing" [1]=> string(4) "danc" } } */
// 逆序肯定环视 (?<=exp) $reg = "#(?<=\bre)\w+\b#"; $str = "reading a book"; preg_match_all($reg, $str, $m); var_dump($m);
/* array(1) { [0]=> array(1) { [0]=> string(5) "ading" } } */
$reg = "#((?<=\d)\d{3})+\b#"; $str = "12345678901234567890"; preg_match_all($reg, $str, $m); var_dump($m);
/* array(2) { [0]=> array(1) { [0]=> string(18) "345678901234567890" } [1]=> array(1) { [0]=> string(3) "890" } } */
$reg = "#<a[^>]*>([^<>]*)<\/a>#"; $str = '<a href="http://baidu.com">baidu</a>some<a href="http://sohu.com">sohu</a>'; preg_match_all($reg, $str, $m); var_dump($m);
/* array(2) { [0]=> array(2) { [0]=> string(36) "<a href="http://baidu.com">baidu</a>" [1]=> string(34) "<a href="http://sohu.com">sohu</a>" } [1]=> array(2) { [0]=> string(5) "baidu" [1]=> string(4) "sohu" } } */
// 忽略大小写模式 i $reg = "%<div>gg<\/div>%i"; $str = "<div>gG</Div>"; preg_match_all($reg, $str, $m); var_dump($m); /* array(1) { [0]=> array(1) { [0]=> string(13) "<div>gG</Div>" } } */
// 多行模式 m $reg = "%.*reg$%mi"; $reg2 = "%^t.*%mi"; $str = "this is reg reg this is regexp turtor,oh reg"; preg_match_all($reg, $str, $m); preg_match_all($reg2, $str, $m2); var_dump($m, $m2); /* array(1) { [0]=> array(3) { [0]=> string(11) "this is reg" [1]=> string(3) "reg" [2]=> string(20) "regexp turtor,oh reg" } } array(1) { [0]=> array(2) { [0]=> string(11) "this is reg" [1]=> string(7) "this is" } } */
// 点号统配模式 s (s修饰符包括换行符) $str = "<body> <div class='content'> <div class='head'></div> <div class='body'></div> <div class='foot'></div> </div> </body>"; $reg = "|<body>(.*)<\/body>|"; $reg2 = "|<body>(.*)<\/body>|s"; preg_match_all($reg, $str, $m); preg_match_all($reg2, $str, $m2); var_dump($m, $m2); /* array(2) { [0]=> array(0) { } [1]=> array(0) { } } array(2) { [0]=> array(1) { [0]=> string(118) "<body> <div class='content'> <div class='head'></div> <div class='body'></div> <div class='foot'></div> </div> </body>" } [1]=> array(1) { [0]=> string(105) " <div class='content'> <div class='head'></div> <div class='body'></div> <div class='foot'></div> </div> " } } */
// 懒惰匹配,reg和reg2两个表达式等价 $reg = "#\[url\](.*?)\[\/url\]#"; $reg2 = "#\[url\](.*)\[\/url\]#U"; $str = '[url]1.gif[/url][url]2.gif[/url][url]3.gif[/url]'; $s = preg_replace($reg, "<img src=http://image.ai.com/upload/$1>", $str); $s2 = preg_replace($reg2, "<img src=http://image.ai.com/upload/$1>", $str); var_dump($s, $s2);
/* string(126) "<img src=http://image.ai.com/upload/1.gif><img src=http://image.ai.com/upload/2.gif><img src=http://image.ai.com/upload/3.gif>" string(126) "<img src=http://image.ai.com/upload/1.gif><img src=http://image.ai.com/upload/2.gif><img src=http://image.ai.com/upload/3.gif>" */
// 支持 UTF-8 转义表达 u $reg = "#^[\x{4e00}-\x{9fa5}]+$#u"; $str = "php编程"; var_dump("$str 全是中文? ", preg_match_all($reg, $str, $m)); $str = "编程"; var_dump("$str 全是中文? ", preg_match_all($reg, $str, $m)); /* string(24) "php编程 全是中文? " int(0) string(21) "编程 全是中文? " int(1) */
|