PHPRegex函數

來源:互聯網
上載者:User

 關於Regex (Regular expression_r),似乎譯成:字串解析處理,較合乎字面上的意義。但目前所有的相關書籍都譯成Regex或是正規標記法....等等,實際上是負責字串解析比對,並對字串做相關的處理。

ereg: 字串比對解析。
ereg_replace: 字串比對解析並取代。
eregi: 字串比對解析,與大小寫無關。
eregi_replace: 字串比對解析並取代,與大小寫無關。
split: 將字串依指定的規則切開。
sql_regcase: 將字串逐字返回大小寫字元。 ereg

字串比對解析。

文法: bool ereg(string pattern, string string, array [regs]);

傳回值: 布爾值

函數種類: 資料處理

 

 

 
內容說明

 

本函數以 pattern 的規則來解析比對字串 string。比對結果返回的值放在數組參數 regs 之中,regs[0] 內容就是原字串 string、regs[1] 為第一個合乎規則的字串、regs[2] 就是第二個合乎規則的字串,餘類推。若省略參數 regs,則只是單純地比對,找到則傳回值為 true。

 

 

 
使用範例

 

這個例子是 markus@dnet.it 在 14-Jun-1999 所提出的,可對使用者輸入的 E-Mail 作簡單的檢查,檢查使用者的 E-Mail 字串是否有 @ 字元,在 @ 字元前有英文字母或數字,在之後有數節字串,最後的小數點後只能有二個或三個英文字母。super@mail.wilson.gs 就可以通過檢查,super@mail.wilson 就不能通過檢查。

<?php
if (eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",$email)) {
    echo "您的 E-Mail 通過初步檢查";
}
?>

 

  ereg_replace

字串比對解析並取代。

文法: string ereg_replace(string pattern, string replacement, string string);

傳回值: 字串

函數種類: 資料處理

 

 

 
內容說明

 

本函數以 pattern 的規則來解析比對字串 string,欲取而代之的字串為參數 replacement。傳回值為字串類型,為取代後的字串結果。

 

 

 
使用範例

 

ken@freebsdrocks.com 在 16-Mar-1999 提出的例子。

<?php
$text = 'This is a {1} day, not {2} and {3}.';
$daytype = array( 1 => 'fine',
                  2 => 'overcast',
                  3 => 'rainy' );
while (ereg ('{([0-9]+)}', $text, $regs)) {
  $found = $regs[1];
  $text = ereg_replace("\{".$found."\}", $daytype[$found], $text);
}
echo "$text\n";
// This is a fine day, not overcast and rainy.
?>

ken@freebsdrocks.com 並同時提出具有相同功能的perl 程式範例如下:

$text = 'This is a {1} day, not {2} and {3}.';
%daytype = ( 1 => 'fine',
               2 => 'overcast',
               3 => 'rainy' );
$text =~ s/{(\d+)}/$daytype{$1}/eg;
print "$text\n";

結果是:'This is a fine day, not overcast and rainy.

 

  eregi

字串比對解析,與大小寫無關。

文法: int eregi(string pattern, string string, array [regs]);

傳回值: 整數/數組

函數種類: 資料處理

 

 

 
內容說明

 

本函數和 ereg() 類似,用法也相同。不同之處在於 ereg() 有區分大小寫,本函數與大小寫無關。 eregi_replace

字串比對解析並取代,與大小寫無關。

文法: string eregi_replace(string pattern, string replacement, string string);

傳回值: 字串

函數種類: 資料處理

 

 

 
內容說明

 

本函數和 ereg_replace() 類似,用法也相同。不同之處在於 ereg_replace() 有區分大小寫,本函數與大小寫無關。

 

  split

將字串依指定的規則切開。

文法: array split(string pattern, string string, int [limit]);

傳回值: 數組

函數種類: 資料處理

 

 

 
內容說明

 

本函數可將字串依指定的規則分開。切開後的傳回值為陣列變數。參數 pattern 為指定的規則字串、參數 string 則為待處理的字串、參數 limit 可省略,表示欲處理的最多合乎值。值得注意的是本函數的 pattern 參數有區分大小寫。 sql_regcase

將字串逐字返回大小寫字元。

文法: string sql_regcase(string string);

傳回值: 數組

函數種類: 資料處理

 

 

 
內容說明

 

本函數可將字串之字元逐字返回大小寫。在 PHP 使用上,本函數沒有什麼作用,但可能可以提供外部程式或資料庫處理。

 

 

 
使用範例

 

例中的返回字串為 [Ww][Ii][Ll][Ss][Oo][Nn]

<?php
print(sql_regcase("wilson"));
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.