php正則表達式實例

發佈:seasun   來源:網絡   閱讀:31  【

收集了10個php正則表達式實例,希望對大家學習正則有所幫助。

1. 根據購物車中的商品Item ID,準確判斷客人的原裝機型號。
 

複製代碼 代碼如下:
<?php
$title = "Replacement Canon BP-511 Camcorder Battery [Item ID:3-236-523]";
if( eregi("([Item ID:)([0-9]+)-([0-9]+)-([0-9]+)(])",$title,$arr) ){
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}
?>

 

2. 匹配日期
 

複製代碼 代碼如下:
<?php
// 分隔符可以是斜線,點,或橫線
$date = "04/30/1973";
list($month, $day, $year) = split ('[/.-]', $date); // 漏洞: 04/30-1973也能匹配得上
echo "Month: $month; Day: $day; Year: $year<br />n";
?>

 

3. 搜索單詞 web
 

複製代碼 代碼如下:
<?php
if (preg_match ("/bwebb/i", "PHP is the website scripting language of choice.")) {
    print "A match was found.";
} else {
    print "A match was not found.";
}
?>

 

4. 從url 中取出域名
 

複製代碼 代碼如下:
<?php
// 從 URL 中取得主機名
//by http://www.jbxue.com

preg_match("/^(http://)?([^/]+)/i",
    "http://www.php.net/index.html", $matches);
$host = $matches[2];
// 從主機名中取得後面兩段
preg_match("/[^./]+.[^./]+$/", $host, $matches);
echo "domain name is: {$matches[0]}n";
// 本例執行後將輸出: domain name is: php.net
?>

 

5. 你知道下面的程序輸出什麼?
 

複製代碼 代碼如下:
<?php
preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
    "<b>example: </b><div align=left>this is a test</div>",
    $out, PREG_PATTERN_ORDER); // 注意PREG_PATTERN_ORDER和PREG_SET_ORDER的區別
print $out[0][0].", ".$out[0][1]."n";
print $out[1][0].", ".$out[1][1]."n";
echo "<pre>";
print_r($out);
?>

 

6. 功能: 轉義正則表達式字符
 

複製代碼 代碼如下:
<?php
$keywords = "$40 for a g3/400";
$keywords = preg_quote ($keywords, "/");
echo $keywords; // returns $40 for a g3/400
?>

7. 看人家如何用 preg_quote() 函數實現高亮顯示
複製代碼 代碼如下:
<?php
// 本例中,preg_quote($word) 用來使星號不在正則表達式中
// 具有特殊含義。
$textbody = "This book is *very* difficult to find.";
$word = "*very*";
$textbody = preg_replace ("/".preg_quote($word)."/",
                          "<b>".$word."</b>",
                          $textbody);
echo $textbody;
?>

 

8. 用回調函數執行正則表達式的搜索和替換
 

複製代碼 代碼如下:
<?php
  // 此文本是用於 2002 年的,
  // 現在想使其能用於 2003 年
  $text = "April fools day is 04/01/2002n";
  $text.= "Last christmas was 12/24/2001n";
  // 回調函數
  function next_year($matches) {
    // 通常:$matches[0] 是完整的匹配項
    // $matches[1] 是第一個括號中的子模式的匹配項
    // 以此類推
    return $matches[1].($matches[2]+1);
  }
  echo preg_replace_callback(
              "|(d{2}/d{2}/)(d{4})|",
              "next_year",
              $text);
  // 結果爲:
  // April fools day is 04/01/2003
  // Last christmas was 12/24/2002
?>

 

9. 在 preg_replace() 中使用索引數組
 

複製代碼 代碼如下:
<?php
$string = "The quick brown fox jumped over the lazy dog.";
$patterns[0] = "/quick/";
$patterns[1] = "/brown/";
$patterns[2] = "/fox/";
$replacements[2] = "bear";
$replacements[1] = "black";
$replacements[0] = "slow";
print preg_replace($patterns, $replacements, $string);
/* Output
    ======
The bear black slow jumped over the lazy dog.
*/
/* By ksorting patterns and replacements,
    we should get what we wanted. */
ksort($patterns);
ksort($replacements);
print preg_replace($patterns, $replacements, $string);
/* Output
    ======
The slow black bear jumped over the lazy dog.
*/
?>

 

10. 將 HTML 轉換成文本

複製代碼 代碼如下:
<?php
// $document 應包含一個 HTML 文檔。
// 本例將去掉 HTML 標記,# 代碼
// 和空白字符。還會將一些通用的
// HTML 實體轉換成相應的文本。
$search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 #
                 "'<[/!]*?[^<>]*?>'si", // 去掉 HTML 標記
                 "'([rn])[s]+'", // 去掉空白字符
                 "'&(quot|#34);'i", // 替換 HTML 實體
                 "'&(amp|#38);'i",
                 "'&(lt|#60);'i",
                 "'&(gt|#62);'i",
                 "'&(nbsp|#160);'i",
                 "'&(iexcl|#161);'i",
                 "'&(cent|#162);'i",
                 "'&(pound|#163);'i",
                 "'&(copy|#169);'i",
                 "'&#(d+);'e"); // 作爲 PHP 代碼運行
$replace = array ("",
                  "",
                  "1",
                  """,
                  "&",
                  "<",
                  ">",
                  " ",
                   chr(161),
                   chr(162),
                   chr(163),
                   chr(169),
                  "chr(1)");
$text = preg_replace ($search, $replace, $document);
?>
本文出處參考:http://www.jbxue.com/article/640.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章