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
<?php
/* -----------------------------------------------------------------------
 *  正規表現のpreg_matchの【最短一致と言明サブパターン】と拡張子取得の例
 *
 *  作成: 2015.07.28 yoshi of CXMedia Inc.
 * ----------------------------------------------------------------------- */
header("Content-type:text/plain; charset=UTF-8");

$str "124";
preg_match("/123?4/",$str,$matches);
print_r($matches);

$str "124";
preg_match("/123+?/",$str,$matches);
print_r($matches);

$str "124";
preg_match("/123??4/",$str,$matches);
print_r($matches);

// ファイルパス有り無しのサブパターン「(?:[\:\.\/a-z0-9]*)」でパスをスキップして、
// 後方からの'\.'までの文字列を参照して拡張子を取得する
$path '../damy/sample/test.txt';
preg_match("/(?:[\:\.\/a-z0-9]*)\.(.+?)$/i",$path,$matches);
echo 
"\n■ファイルパスの拡張子を取得\n";
print_r($matches);
?>