PHP 18:data_valid_fns.php 看Regex

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

原文:PHP 18:data_valid_fns.php 看Regex

本章介紹Regex。
先看看data_valid_fns.php的代碼吧。
 1 <?php
 2 
 3 function filled_out($form_vars)
 4 {
 5   // test that each variable has a value
 6   foreach ($form_vars as $key => $value)
 7   {
 8      if (!isset($key) || ($value == ‘‘)) 
 9         return false;
10   } 
11   return true;
12 }
13 
14 function valid_email($address)
15 {
16   // check an email address is possibly valid
17   if (ereg(‘^[a-zA-Z0-9_\.\-][email protected][a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$‘, $address))
18     return true;
19   else 
20     return false;
21 }
22 
23 ?>
24 簡要介紹一下。
filled_out($form_vars)驗證輸入的有效性。
valid_email($address)驗證email的有效性。它是通過Regex進行的。
本章具體介紹Regex。
PHP支援POSIX擴充的Regex,也支援使用Perl文法相容的Regex。
如果你對Regex不是很清楚的話,請參見Regex的文法。
下面我們分別介紹以下這2種格式的Regex
1 POSIXRegex函數
   為了使用此功能,你首先要配置PHP,使之支援它。在Windows下安裝的話,它會自動設定。但是在其他動作系統下,需要手工配置。
要啟用 regexp 的支援,請在配置 PHP 時加上 --with-regex[=TYPE]。TYPE 可以是system,apache 或 php 之一。預設使用 php。
 支援的函數有:

  •   ereg_replace -- Regex替換
  •   ereg -- Regex匹配
  •   eregi_replace -- 不區分大小寫Regex替換
  •   eregi -- 不區分大小寫Regex匹配
  •   split -- 用Regex將字串分割到數組中
  •   spliti -- 用Regex不區分大小寫將字串分割到數組中
  •   sql_regcase -- 產生用於不區分大小的匹配的Regex

2 Perl相容Regex函數
  • preg_grep --  返回與模式比對的數組單元
  • preg_match_all -- 進行全域Regex匹配
  • preg_match -- 進行Regex匹配
  • preg_quote -- 轉義Regex字元
  • preg_replace_callback -- 用回呼函數執行Regex的搜尋和替換
  • preg_replace -- 執行Regex的搜尋和替換
  • preg_split -- 用Regex分割字串

PHP 18:data_valid_fns.php 看Regex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.