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
<?php
/* --------------------------------------------------------------------------------
 *       正規表現のpreg_matchの例
 *    数字と英字の一致、前文字あるなしの一致、文字先頭と文末の一致、パターン一致
 *
 *  作成: 2015.07.23 yoshi of CXMedia Inc.
 * -------------------------------------------------------------------------------- */
header("Content-type:text/plain; charset=UTF-8");

// 数字と英字の一致した配列表示
preg_match("/^(\d{3})([a-z]+)/","123abc",$matches);
echo 
"数字と英字の一致した配列表示\n";
print_r($matches);

// ?の前文字あるなしの一致した配列表示
preg_match("/^(https?)\s+(https?)/","http https",$matches);
echo 
"?の前文字あるなしの一致した配列表示\n";
print_r($matches);

//「^」先頭から始まる空白と文字列の後の空白の文末一致($)した配列表示
$str "   ab c   ";
preg_match("/^\s*(.*)\s+$/",$str,$matches);
echo 
"「^」先頭から始まる空白と文字列の後の空白の文末一致(\$)した配列表示\n";
print_r($matches);

// (?:xxx|yyy)のパターン一致した配列表示
preg_match("/^(?:http|https)\s(?:http|https)/","http https",$matches);
echo 
"(?:xxx|yyy)のパターン一致した配列表示\n";
print_r($matches);
?>