<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # PHP 正則表達式 > 原文: [https://zetcode.com/lang/php/regex/](https://zetcode.com/lang/php/regex/) 在 PHP 教程的這一部分中,我們介紹了 PHP 中的正則表達式。 正則表達式用于文本搜索和更高級的文本操作。 正則表達式是內置工具,如`grep`,`sed`,文本編輯器(如 vi,emacs),編程語言(如 Tcl,Perl 和 Python)。 PHP 也具有對正則表達式的內置支持。 在 PHP 中,有兩個用于正則表達式的模塊:POSIX Regex 和 PCRE。 POSIX 正則表達式已貶值。 在本章中,我們將使用 PCRE 示例。 PCRE 代表與 Perl 兼容的正則表達式。 使用正則表達式時,需要做兩件事:正則表達式函數和模式。 模式是一個正則表達式,用于定義我們正在搜索或操縱的文本。 它由文本文字和元字符組成。 該模式放置在兩個定界符內。 這些通常是`//`,`##`或`@@`字符。 它們通知正則表達式函數模式的開始和結束位置。 這是 PCRE 中使用的部分元字符列表。 | | | | --- | --- | | `.` | 匹配任何單個字符。 | | `*` | 與前面的元素匹配零次或多次。 | | `[ ]` | 括號表達。 與方括號內的字符匹配。 | | `[^ ]` | 匹配方括號中未包含的單個字符。 | | `^` | 匹配字符串中的起始位置。 | | `$` | 匹配字符串中的結束位置。 | | <code>&#124;</code> | 備用運算符。 | ## PRCE 函數 我們定義一些 PCRE regex 函數。 它們都有一個`preg`前綴。 * `preg_split()` - 按正則表達式模式分割字符串 * `preg_match()` - 執行正則表達式匹配 * `preg_replace()` - 用正則表達式模式搜索和替換字符串 * `preg_grep()` - 返回與正則表達式模式匹配的數組條目 接下來,我們將為每個函數提供一個示例。 ```php php> print_r(preg_split("@\s@", "Jane\tKate\nLucy Marion")); Array ( [0] => Jane [1] => Kate [2] => Lucy [3] => Marion ) ``` 我們有四個名字,用空格分開。 `\s`是代表空格的字符類。 `preg_split()`函數返回數組中的拆分字符串。 ```php php> echo preg_match("#[a-z]#", "s"); 1 ``` `preg_match()`函數查看字符類`[a-z]`中是否包含`'s'`字符。 該類代表從`a`到`z`的所有字符。 成功返回 1。 ```php php> echo preg_replace("/Jane/","Beky","I saw Jane. Jane was beautiful."); I saw Beky. Beky was beautiful. ``` `preg_replace()`函數將單詞`"Jane"`的所有出現替換為單詞`"Beky"`。 ```php php> print_r(preg_grep("#Jane#", ["Jane", "jane", "Joan", "JANE"])); Array ( [0] => Jane ) ``` `preg_grep()`函數返回與給定模式匹配的單詞數組。 在此示例中,數組中僅返回一個單詞。 這是因為默認情況下,搜索區分大小寫。 ```php php> print_r(preg_grep("#Jane#i", ["Jane", "jane", "Joan", "JANE"])); Array ( [0] => Jane [1] => jane [3] => JANE ) ``` 在此示例中,我們執行不區分大小寫的`grep`。 我們將`i`修飾符放在右定界符之后。 現在,返回的數組包含三個單詞。 ## 點元字符 `.`(點)元字符代表文本中的任何單個字符。 `single.php` ```php <?php $words = [ "Seven", "even", "Maven", "Amen", "Leven" ]; $pattern = "/.even/"; foreach ($words as $word) { if (preg_match($pattern, $word)) { echo "$word matches the pattern\n"; } else { echo "$word does not match the pattern\n"; } } ``` 在`$words`數組中,我們有五個詞。 ```php $pattern = "/.even/"; ``` 在這里,我們定義搜索模式。 模式是一個字符串。 正則表達式位于分隔符內。 分隔符是強制性的。 在我們的例子中,我們使用正斜杠`/ /`作為分隔符。 請注意,如果需要,我們可以使用不同的定界符。 點字符代表任何單個字符。 ```php if (preg_match($pattern, $word)) { echo "$word matches the pattern\n"; } else { echo "$word does not match the pattern\n"; } ``` 如果這五個詞與模式匹配,我們將對其進行測試。 ```php $ php single.php Seven matches the pattern even does not match the pattern Maven does not match the pattern Amen does not match the pattern Leven matches the pattern ``` 七個和七個詞與我們的搜索模式匹配。 ## 錨點 錨點匹配給定文本內字符的位置。 在下一個示例中,我們查看字符串是否位于句子的開頭。 `anchors.php` ```php <?php $sentence1 = "Everywhere I look I see Jane"; $sentence2 = "Jane is the best thing that happened to me"; if (preg_match("/^Jane/", $sentence1)) { echo "Jane is at the beginning of the \$sentence1\n"; } else { echo "Jane is not at the beginning of the \$sentence1\n"; } if (preg_match("/^Jane/", $sentence2)) { echo "Jane is at the beginning of the \$sentence2\n"; } else { echo "Jane is not at the beginning of the \$sentence2\n"; } ``` 我們有兩個句子。 模式是`^Jane`。 該模式檢查'Jane'字符串是否位于文本的開頭。 ```php $ php anchors.php Jane is not at the beginning of the $sentence1 Jane is at the beginning of the $sentence2 ``` ```php php> echo preg_match("#Jane$#", "I love Jane"); 1 php> echo preg_match("#Jane$#", "Jane does not love me"); 0 ``` `Jane$`模式與字符串`"Jane"`結尾的字符串匹配。 ## 完全匹配 在以下示例中,我們顯示了如何查找完全匹配的單詞。 ```php php> echo preg_match("/mother/", "mother"); 1 php> echo preg_match("/mother/", "motherboard"); 1 php> echo preg_match("/mother/", "motherland"); 1 ``` `mother`模式適合單詞`mother`,`motherboard`和`motherland`。 說,我們只想查找完全匹配的單詞。 我們將使用前面提到的錨點`^`和`$`字符。 ```php php> echo preg_match("/^mother$/", "motherland"); 0 php> echo preg_match("/^mother$/", "Who is your mother?"); 0 php> echo preg_match("/^mother$/", "mother"); 1 ``` 使用定位字符,我們可以獲得與模式完全匹配的單詞。 ## 量詞 標記或組之后的量詞指定允許前面的元素出現的頻率。 ```php ? - 0 or 1 match * - 0 or more + - 1 or more {n} - exactly n {n,} - n or more {,n} - n or less (??) {n,m} - range n to m ``` 上面是常見量詞的列表。 問號`?`表示存在零或前一個元素之一。 `zeroorone.php` ```php <?php $words = [ "color", "colour", "comic", "colourful", "colored", "cosmos", "coloseum", "coloured", "colourful" ]; $pattern = "/colou?r/"; foreach ($words as $word) { if (preg_match($pattern, $word)) { echo "$word matches the pattern\n"; } else { echo "$word does not match the pattern\n"; } } ``` `$words`數組中有四個 9。 ```php $pattern = "/colou?r/"; ``` 顏色用于美式英語,顏色用于英式英語。 此模式匹配兩種情況。 ```php $ php zeroorone.php color matches the pattern colour matches the pattern comic does not match the pattern colourful matches the pattern colored matches the pattern cosmos does not match the pattern coloseum does not match the pattern coloured matches the pattern colourful matches the pattern ``` 這是`zeroorone.php`腳本的輸出。 `*`元字符與前面的元素匹配零次或更多次。 `zeroormore.php` ```php <?php $words = [ "Seven", "even", "Maven", "Amen", "Leven" ]; $pattern = "/.*even/"; foreach ($words as $word) { if (preg_match($pattern, $word)) { echo "$word matches the pattern\n"; } else { echo "$word does not match the pattern\n"; } } ``` 在上面的腳本中,我們添加了`*`元字符。 `.*`組合表示零個,一個或多個單個字符。 ```php $ php zeroormore.php Seven matches the pattern even matches the pattern Maven does not match the pattern Amen does not match the pattern Leven matches the pattern ``` 現在該模式匹配三個單詞:`Seven`,`even`和`Leven`。 ```php php> print_r(preg_grep("#o{2}#", ["gool", "root", "foot", "dog"])); Array ( [0] => gool [1] => root [2] => foot ) ``` `o{2}`模式匹配恰好包含兩個'o'字符的字符串。 ```php php> print_r(preg_grep("#^\d{2,4}$#", ["1", "12", "123", "1234", "12345"])); Array ( [1] => 12 [2] => 123 [3] => 1234 ) ``` 我們有這個`^\d{2,4}$`模式。 `\d`是一個字符集; 它代表數字。 該模式匹配具有 2、3 或 4 位數字的數字。 ## 交替 下一個示例說明了交替運算符`|`。 該運算符使您可以創建具有多種選擇的正則表達式。 `alternation.php` ```php <?php $names = [ "Jane", "Thomas", "Robert", "Lucy", "Beky", "John", "Peter", "Andy" ]; $pattern = "/Jane|Beky|Robert/"; foreach ($names as $name) { if (preg_match($pattern, $name)) { echo "$name is my friend\n"; } else { echo "$name is not my friend\n"; } } ``` `$names`數組中有八個名稱。 ```php $pattern = "/Jane|Beky|Robert/"; ``` 這是搜索模式。 該模式查找`"Jane"`,`"Beky"`或`"Robert"`字符串。 ```php $ php alternation.php Jane is my friend Thomas is not my friend Robert is my friend Lucy is not my friend Beky is my friend John is not my friend Peter is not my friend Andy is not my friend ``` 這是腳本的輸出。 ## 子模式 我們可以使用方括號`()`在樣式內創建子模式。 ```php php> echo preg_match("/book(worm)?$/", "bookworm"); 1 php> echo preg_match("/book(worm)?$/", "book"); 1 php> echo preg_match("/book(worm)?$/", "worm"); 0 ``` 我們有以下正則表達式模式:`book(worm)?$`。 `(worm)`是子模式。 ? 字符跟隨子模式,這意味著該子模式在最終模式中可能出現 0、1 次。 這里的`$`字符用于字符串的精確末尾匹配。 沒有它,諸如`bookstore`,`bookmania`之類的詞也將匹配。 ```php php> echo preg_match("/book(shelf|worm)?$/", "book"); 1 php> echo preg_match("/book(shelf|worm)?$/", "bookshelf"); 1 php> echo preg_match("/book(shelf|worm)?$/", "bookworm"); 1 php> echo preg_match("/book(shelf|worm)?$/", "bookstore"); 0 ``` 子模式經常交替使用。 `(shelf|worm)`子模式可創建多個單詞組合。 ## 字符類 我們可以使用方括號將字符組合成字符類。 字符類與括號中指定的任何字符匹配。 `characterclass.php` ```php <?php $words = [ "sit", "MIT", "fit", "fat", "lot" ]; $pattern = "/[fs]it/"; foreach ($words as $word) { if (preg_match($pattern, $word)) { echo "$word matches the pattern\n"; } else { echo "$word does not match the pattern\n"; } } ``` 我們用兩個字符定義一個字符集。 ```php $pattern = "/[fs]it/"; ``` 這是我們的模式。 `[fs]`是字符類。 請注意,我們一次只能處理一個字符。 我們要么考慮`f`要么`s`,但不能同時考慮兩者。 ```php $ php characterclass.php sit matches the pattern MIT does not match the pattern fit matches the pattern fat does not match the pattern lot does not match the pattern ``` 這是腳本的結果。 我們還可以將速記元字符用于字符類。 `\w`代表字母數字字符,`\d`代表數字,`\s`空格字符。 `shorthand.php` ```php <?php $words = [ "Prague", "111978", "terry2", "mitt##" ]; $pattern = "/\w{6}/"; foreach ($words as $word) { if (preg_match($pattern, $word)) { echo "$word matches the pattern\n"; } else { echo "$word does not match the pattern\n"; } } ``` 在上面的腳本中,我們測試包含字母數字字符的單詞。 `\w{6}`代表六個字母數字字符。 僅單詞`mitt##`不匹配,因為它包含非字母數字字符。 ```php php> echo preg_match("#[^a-z]{3}#", "ABC"); 1 ``` `#[^a-z]{3}#`模式代表三個不在`a-z`類中的字符。 `"ABC"`字符符合條件。 ```php php> print_r(preg_grep("#\d{2,4}#", [ "32", "234", "2345", "3d3", "2"])); Array ( [0] => 32 [1] => 234 [2] => 2345 ) ``` 在上面的示例中,我們有一個匹配 2、3 和 4 位數字的模式。 ## 提取匹配 `preg_match()`具有可選的第三個參數。 如果提供,則將其填充為搜索結果。 變量是一個數組,其第一個元素包含與完整模式匹配的文本,第二個元素包含第一個捕獲的帶括號的子模式,依此類推。 `extract_matches.php` ```php <?php $times = [ "10:10:22", "23:23:11", "09:06:56" ]; $pattern = "/(\d\d):(\d\d):(\d\d)/"; foreach ($times as $time) { $r = preg_match($pattern, $time, $match); if ($r) { echo "The $match[0] is split into:\n"; echo "Hour: $match[1]\n"; echo "Minute: $match[2]\n"; echo "Second: $match[3]\n"; } } ``` 在示例中,我們提取時間字符串的一部分。 ```php $times = [ "10:10:22", "23:23:11", "09:06:56" ]; ``` 我們在英語語言環境中有三個時間字符串。 ```php $pattern = "/(\d\d):(\d\d):(\d\d)/"; ``` 模式使用方括號分為三個子模式。 我們想確切地指每個部分。 ```php $r = preg_match($pattern, $time, $match); ``` 我們將第三個參數傳遞給`preg_match()`函數。 如果匹配,則包含匹配字符串的文本部分。 ```php if ($r) { echo "The $match[0] is split into:\n"; echo "Hour: $match[1]\n"; echo "Minute: $match[2]\n"; echo "Second: $match[3]\n"; } ``` `$match[0]`包含與完整模式匹配的文本,`$match[1]`包含與第一個子模式匹配的文本,`$match[2]`與第二個子模式匹配,`$match[3]`與第三個子模式匹配。 ```php $ php extract_matches.php The 10:10:22 is split into: Hour: 10 Minute: 10 Second: 22 The 23:23:11 is split into: Hour: 23 Minute: 23 Second: 11 The 09:06:56 is split into: Hour: 09 Minute: 06 Second: 56 ``` 這是示例的輸出。 ## 電子郵件示例 接下來有一個實際的例子。 我們創建一個正則表達式模式來檢查電子郵件地址。 `emails.php` ```php <?php $emails = [ "luke@gmail.com", "andy@yahoocom", "34234sdfa#2345", "f344@gmail.com"]; # regular expression for emails $pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,18}$/"; foreach ($emails as $email) { if (preg_match($pattern, $email)) { echo "$email matches \n"; } else { echo "$email does not match\n"; } } >? ``` 請注意,此示例僅提供一種解決方案。 它不一定是最好的。 ```php $pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,18}$/"; ``` 這就是模式。 這里的第一個`^`和最后一個`$`字符可得到精確的模式匹配。 模式前后不允許有字符。 電子郵件分為五個部分。 第一部分是本地部分。 這通常是公司,個人或昵稱的名稱。 `[a-zA-Z0-9._-]+`列出了所有可能的字符,我們可以在本地使用。 它們可以使用一次或多次。 第二部分是文字`@`字符。 第三部分是領域部分。 通常是電子郵件提供商的域名,例如 yahoo 或 gmail。 `[a-zA-Z0-9-]+`是一個字符集,提供了可在域名中使用的所有字符。 `+`量詞使用這些字符中的一個或多個。 第四部分是點字符。 它前面有轉義字符(`\`)。 這是因為點字符是一個元字符,并且具有特殊含義。 通過轉義,我們得到一個文字點。 最后一部分是頂級域。 模式如下:`[a-zA-Z.]{2,18}`頂級域可以包含 2 到 18 個字符,例如`sk, net, info, travel, cleaning, travelinsurance`。 最大長度可以為 63 個字符,但是今天大多數域都少于 18 個字符。 還有一個點字符。 這是因為某些頂級域包含兩個部分: 例如`co.uk`。 ```php $ php emails.php luke@gmail.com matches andy@yahoocom does not match 34234sdfa#2345 does not match f344@gmail.com matches ``` 這是`emails.php`示例的輸出。 ## 總結 最后,我們快速回顧一下正則表達式模式。 ```php Jane the 'Jane' string ^Jane 'Jane' at the start of a string Jane$ 'Jane' at the end of a string ^Jane$ exact match of the string 'Jane' [abc] a, b, or c [a-z] any lowercase letter [^A-Z] any character that is not a uppercase letter (Jane|Becky) Matches either 'Jane' or 'Becky' [a-z]+ one or more lowercase letters ^[98]?$ digits 9, 8 or empty string ([wx])([yz]) wy, wz, xy, or xz [0-9] any digit [^A-Za-z0-9] any symbol (not a number or a letter) ``` 在本章中,我們介紹了 PHP 中的正則表達式。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看