<? preg_match_all ("|<[^>]+>(.*)</[^>]+>|U", "<b>example: </b><div align=left>this is a test</div>", $out, PREG_PATTERN_ORDER); print $out[0][0].", ".$out[0][1]."n"; print $out[1][0].", ".$out[1][1]."n"; ?> |
<b>example: </b>, <div align=left>this is a test</div> example: , this is a test |
<? preg_match_all ("|<[^>]+>(.*)</[^>]+>|U", "<b>example: </b><div align=left>this is a test</div>", $out, PREG_SET_ORDER); print $out[0][0].", ".$out[0][1]."n"; print $out[1][0].", ".$out[1][1]."n"; ?> |
<b>example: </b>, example: <div align=left>this is a test</div>, this is a test |
<? preg_match_all("/(?(d{3})?)?(?(1)[-s])d{3}-d{4}/x","Call 555-1212 or 1-800-555-1212", $phones); ?> |
<? // \2 是一个逆向引用的例子,其在 PCRE 中的含义是 // 必须匹配正则表达式本身中第二组括号内的内容,本例中 // 就是 ([w]+)。因为字符串在双引号中,所以需要 // 多加一个反斜线。 $html = "<b>bold text</b><a href=howdy.html>click me</a>"; preg_match_all ("/(<([w]+)[^>]*>)(.*)(</\2>)/", $html, $matches); for ($i=0; $i< count($matches[0]); $i++) { echo "matched: ".$matches[0][$i]."n"; echo "part 1: ".$matches[1][$i]."n"; echo "part 2: ".$matches[3][$i]."n"; echo "part 3: ".$matches[4][$i]."nn"; } ?> |
matched: <b>bold text</b> part 1: <b> part 2: bold text part 3: </b> matched: <a href=howdy.html>click me</a> part 1: <a href=howdy.html> part 2: click me part 3: </a> |