PHP正則表達式函數(shù)學(xué)習(xí)小結(jié)
PHP正則表達式函數(shù)都有那些呢?那么PHP正則表達式函數(shù)具體的是代表什么呢?具體的用法又是什么呢?讓我們一一向你介紹。PHP正則表達式主要用于復(fù)雜字符串的處理。其主要的PHP正則表達式函數(shù)如下:
◆ereg()
◆ereg_replace()
◆eregi()
◆eregi_replace()
◆split()
PHP正則表達式函數(shù)用法歸納:
(1)ereg,eregi
這是正規(guī)表達式匹配函數(shù),前者是大小寫有關(guān)匹配,后者則是無關(guān)的.
用法:
ereg(正規(guī)表達式,字符串,[匹配部分數(shù)組名]); PHP3.0中的正規(guī)表達式大體類似于grep中用的.
(2)ereg_replace,eregi_replace
這些是替換函數(shù).
用法:
ereg_replace(正規(guī)表達式,替換串,原字符串);
字符串處理函數(shù)中有一個strtr,是"翻譯"函數(shù),類似于Perl中的tr/.../.../,
用法:
strtr(字符串,"從","到");
例如:
strtr("aaabb","ab","cd")返回"cccdd".
(3)split
與explode函數(shù)有些類似,但這次可以在匹配某正規(guī)表達式的地方分割字符串.
用法:
split(正規(guī)表達式,字符串,[取出前多少項]);
這些函數(shù)都使用正則字符串做為第一個參數(shù)。PHP使用Posix 1003.2標準所定義的擴展正則字符串。要查考Posix正則表達式的完整描述請看PHP軟件包中regex目錄下的man頁。
PHP正則表達式函數(shù)應(yīng)用實例:
- ereg("abc",$string);
- /* Returns true if "abc" is
- found anywhere in $string. */
- ereg("^abc",$string);
- /* Returns true if "abc" is
- found at the beginning of $string. */
- ereg("abc$",$string);
- /* Returns true if "abc" is
- found at the end of $string. */
- eregi("(ozilla.[23]|MSIE.3)",$HTTP_USER_AGENT);
- /* Returns true if client browser
- is Netscape 2, 3 or MSIE 3. */
- ereg("([[:alnum:]]+) ([[:alnum:]]+)
- ([[:alnum:]]+)",$string,$regs);
- /* Places three space separated words
- into $regs[1], $regs[2] and $regs[3]. */
- ereg_replace("^","",$string);
- /* Put a tag at the beginning of $string. */
- ereg_replace("$","",$string);
- /* Put a tag at the end of $string. */
- ereg_replace(" ","",$string);
- /* Get rid of any carriage
- return characters in $string. */
PHP正則表達式函數(shù)的相關(guān)內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)PHP正則表達式函數(shù)有所幫助。
【編輯推薦】