1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
/* ----------------------------------------------------
* 正規表現のpreg_matchの例2
* HTMLのドメイン形式、メールアドレスの形式チェック
*
* 作成: 2015.07.23 yoshi of CXMedia Inc.
* ---------------------------------------------------- */
header("Content-type: text/plain; charset=UTF-8");
// HTMLのdomain用の正規表現
$html_domain_reg1 = '|(https?):[/]{2}(.[^/]*)?/|i';
// ポート番号なしのURLチェック
$html_domain_reg2 = '|(https?)://([^:/]+)(/.*)?|i';
//「 (?::(\d+))?」はポート番号付きのURLの対応
$html_domain_reg3 = '|(https?)://([^:/]+)(?::(\d+))?(/.*)?|i';
//メールアドレスの正規表現
$mailadr_reg1 = "|^([^@]+)@([a-z_-]+)\.([a-z0-9_-]+)(\..*)*$|i";
$mailadr_reg2 = "/^([a-z0-9_\.\-]+)?@([a-z0-9_\-]+)\.([a-z0-9_\-]+)+\.*([a-z0-9_\.\-]+)?$/i";
//対象の文字列
$test_url1 = "https://www2.cxmedia.co.jp/";
$test_url2 = "http://www.cxmedia.co.jp:80/";
$test_mail1 = "DAMY.cxm-y@xz.docomo.ne.jp";
$test_mail2 = "cxm-y@xxx.sample.co.jp";
echo "HTMLのdomain用URLの形式チェック\n";
preg_match($html_domain_reg1,$test_url1,$matches);
print_r($matches);
echo "ポート番号付きのURLの形式チェック\n";
preg_match($html_domain_reg3,$test_url2,$matches);
print_r($matches);
echo "メールアドレスの形式チェック1\n";
preg_match($mailadr_reg1,$test_mail1,$matches);
print_r($matches);
echo "メールアドレスの形式チェック2\n";
preg_match($mailadr_reg2,$test_mail2,$matches);
print_r($matches);
?>