標籤:style http io for ar cti line new sp
Note:
反斜線在單引號字串和雙引號字串 中都有特殊含義,因此要匹配一個反斜線, 模式中必須寫為 ”\\\\”。 譯註: “/\\/”, 首先它作為字串,反斜線會進行轉義, 那麼轉義後的結果是/\/,這個才是Regex引擎拿到的模式, 而Regex引擎也認為 \ 是轉義標記,它會將分隔字元 / 進行轉義, 從而得到的是一個錯誤,因此,需要 4 個反斜線才可以匹配一個反斜線。
事實上
$attribute = "sdfsdf.555";preg_match(‘/([\w]+)(\.)([0-5]|.)/‘, $attribute, $matches);Array( [0] => sdfsdf.555 //匹配([\w]+)(\.)([0-5]|.) 如果整個模式比對失敗則不會繼續匹配.例如$attribute = "sssss555" [1] => sdfsdf //匹配(\w+) [2] => . //匹配(\.) [3] => 555 //匹配([0-5]*))
preg_replace_callback
將字元竄中Regex匹配到的每一個值用回呼函數處理並返回..
函數會在第一次匹配後 繼續搜尋.
首先會匹配得到[{summary}]
接著會繼續搜尋匹配得到[{items}]
最後匹配得到[{form}]
下個例子..Regex..匹配的結果分別是..[{summary}],[{items}] [{form}]
將三個值用於回呼函數...
比較下面兩個匹配值的不同:
$layout = "{summary}\n{items}\n{form}";$content = preg_replace_callback("/{\\w+}/", function ($matches) {print_r($matches); return $matches[0];}, $layout);print_r($content);$layout = "{summary}-{items}-{form}";$content = preg_replace_callback("/({\\w+})(-)/", function ($matches) {print_r($matches);//["{summary}-","{summary}","-"]return $matches[0];}, $layout);print_r($content);
php pregRegex