無料ブログ作成サービス JUGEM
スポンサーサイト

一定期間更新がないため広告を表示しています

| スポンサードリンク | - | - | - | pookmark |
←prev entry Top next entry→
PHPの正規表現使用例
Jeffrey E.F. Friedl
オライリージャパン
¥ 5,040
(2008-04-26)
コメント:正規表現を勉強しよう

IDEA*IDEA:かなり使えるPHPの正規表現まとめより簡単な正規表現の使い方の例を紹介。元は10 Practical PHP Regular Expression Recipes

上記2サイトの説明では、E-mailのValidationについて注意があり、紹介している方法では不十分とのこと。なので、10 Practical PHP Regular Expression Recipesによると、filter_var関数を利用するといいよ〜と追記で紹介されてます。filter_varはPHP5.2以上じゃないと使えないっぽいけど。。。

※注意:以下の使用例は個人で勉強目的のために使用する程度に留めておくほうが無難だと思われます。

  1. Validate email addresses

    This is only basic email validation. Not recommended to use. Validate perfect email format using regex is not really efficient.

    $email = "test@example.com";
    if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email)) {
    echo "Your email is ok.";
    } else {
    echo "Wrong email address format";
    }

    A better solution for validate email syntax is using filter_var. Thanks to cx42net for pointing me out to filter_var.

    if (filter_var('test+email@fexample.com', FILTER_VALIDATE_EMAIL)) {
    echo "Your email is ok.";
    } else {
    echo "Wrong email address format.";
    }
  2. Validate usernames

    Validate username, consist of alpha-numeric (a-z, A-Z, 0-9), underscores, and has minimum 5 character and maximum 20 character. You could change the minimum character and maximum character to any number you like.

    $username = "user_name12";
    if (preg_match('/^[a-z¥d_]{5,20}$/i', $username)) {
    echo "Your username is ok.";
    } else {
    echo "Wrong username format.";
    }
  3. Validate telephone numbers

    Validate US phone number

    $phone = "(021)423-2323";
    if (preg_match('/¥(?¥d{3}¥)?[-¥s.]?¥d{3}[-¥s.]¥d{4}/x', $phone)) {
    echo "Your phone number is ok.";
    } else {
    echo "Wrong phone number.";
    }
  4. Validate IP addresses

    $IP = "198.168.1.78";
    if (preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',$IP)) {
    echo "Your IP address is ok.";
    } else {
    echo "Wrong IP address.";
    }
  5. Validate postal codes

    Validate US Postal codes

    $zipcode = "12345-5434";
    if (preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zipcode)) {
    echo "Your Zip code is ok.";
    } else {
    echo "Wrong Zip code.";
    }
  6. Validate SSN

    Validate US Sosial Security Number

    $ssn = "333-23-2329";
    if (preg_match('/^[¥d]{3}-[¥d]{2}-[¥d]{4}$/',$ssn)) {
    echo "Your SSN is ok.";
    } else {
    echo "Wrong SSN.";
    }
  7. Validate credit card

    $cc = "378282246310005";
    if (preg_match('/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/', $cc)) {
    echo "Your credit card number is ok.";
    } else {
    echo "Wrong credit card number.";
    }
  8. Validate domain

    $url = "http://komunitasweb.com/";
    if (preg_match('/^(http|https|ftp):¥/¥/([A-Z0-9][A-Z0-9_-]*(?:¥.[A-Z0-9][A-Z0-9_-]*)+):?(¥d+)?¥/?/i', $url)) {
    echo "Your url is ok.";
    } else {
    echo "Wrong url.";
    }
  9. Extract domain name from certain URL

    $url = "http://komunitasweb.com/index.html";
    preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
    $host = $matches[1];
     
    echo $host;
  10. Highlight a word in the content

    $text = "Sample sentence from KomunitasWeb, regex has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor";
     
    $text = preg_replace("/¥b(regex)¥b/i", '<span style="background:#5fc9f6">¥1</span>', $text);
     
    echo $text;

Please note that some of this regex recipe doesn’t really validate to all conditions, therefore not a perfect solution, but it will cover most of the case. It’s not really worth to build perfect validation to all case, and waste your time in the process. Well that just my opinion. Feel free to share your regex magic and your opinion.

上記注意書きのようにあるとおり、これら10の例はすべてのケースに当てはまるわけではないですし、完璧な解決方法ではありません。


JUGEMテーマ:インターネット

| tako | インターネット関連 | comments(0) | trackbacks(0) | pookmark |
スポンサーサイト
| スポンサードリンク | - | - | - | pookmark |
Comment
name:
email:
url:
comments:
Trackback
トラックバック機能は終了しました。