PHPバージョン
4+
/* 文字列から
   一致するすべての正規表現パターンの文字列を検索し、
   その個数を整数で返します。 */

// 対象の文字列
$str = 'apple orange banana apple';

// 検索する正規表現パターン
$pattern = '/apple/';

// 関数を適用し結果を返す
$result = preg_match_all($pattern, $str);
var_dump($result); // 出力: int(2)

/* 一致するすべての部分文字列を
   それぞれの要素で構成された配列として保存することもできます。
   第3引数に割り当てる変数を指定します。 */

// 第3引数に割り当てる変数($matches)を指定
$result = preg_match_all($pattern, $str, $matches);

// 割り当てられた配列を確認
print_r($matches);
/* 出力:
Array
(
    [0] => Array
        (
            [0] => apple
            [1] => apple
        )

)
*/
preg_match_all(
    string $pattern,
    string $subject,
    array &$matches = null,
    int $flags = 0,
    int $offset = 0
): int|false

/* preg_match_all(
    パターン,
    検索対象となる文字列[,
    一致した部分を保存する配列[, 
    追加の設定を指定するフラグ[, 
    検索を開始する文字列内のオフセット]]]
);
*/
$pattern = '/apple/';
$subject = "apple orange banana apple";

$result = preg_match_all($pattern, $subject);

if ($result !== false) {
    var_dump($result); // 出力: int(2)
}
$pattern = '/grape/';
$subject = "apple orange banana apple";

$result = preg_match_all($pattern, $subject);

if ($result !== false) {
    var_dump($result); // 出力: int(0)
}
error_reporting(0); // エラー表示の無効化

$pattern = '/apple'; // 無効な正規表現を使用
$subject = "apple orange banana apple";

$result = preg_match_all($pattern, $subject);

if ($result !== false) {
    var_dump($result);
} else {
    echo 'エラーが発生しました。';
}

// 出力: 'エラーが発生しました。'
// 与えられた文字列から数字を検索
$string = 'これは 123 と 456 を含むサンプルテキストです。';

// パターン:数字を検索
$pattern = '/\d+/';

// $matches という空の配列を宣言し、割り当てるための空間を用意します。
$matches = array();

// $matches を引数に割り当てます。
$num_of_matches = preg_match_all($pattern, $string, $matches);

// 結果の出力
if ($num_of_matches > 0) {
    var_dump($matches); // array(1) { [0]=> array(2) { [0]=> string(3) "123" [1]=> string(3) "456" } }
}
// 例の文字列
$string = "apple orange banana apple";

// 正規表現パターン: 'apple' または 'orange' を検索し、括弧のグループで囲む
$pattern = '/(apple|orange)/';

// PREG_PATTERN_ORDER を使用
// - マッチング結果をパターン(キャプチャグループ)ごとにソート
// - $matches     : 結果を割り当てる配列変数
// - $matches[0] : パターン全体に一致した文字列
// - $matches[1] : 最初の括弧のグループ(キャプチャグループ)に一致した文字列
preg_match_all($pattern, $string, $matches, PREG_PATTERN_ORDER);

// 結果の出力
print_r($matches);

/*
出力:
Array
(
    [0] => Array ( [0] => apple [1] => orange [2] => apple )
    [1] => Array ( [0] => apple [1] => orange [2] => apple )
)
*/
// 例の文字列
$string = "apple orange banana apple";

// 正規表現パターン: 'apple' または 'orange' を検索し、括弧のグループで囲む
$pattern = '/(apple|orange)/';

// PREG_SET_ORDER を使用
// - マッチングした結果を1回ごとのマッチング単位でまとめてソート
// - $matches                    : 結果を割り当てる配列変数
// - $matches[0], $matches[1], … : それぞれ1回ごとのマッチング結果
preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);

// 結果の出力
print_r($matches);

/*
出力:
Array
(
    [0] => Array ( [0] => apple [1] => apple )   // 1回目のマッチング:全体 + 第1グループ
    [1] => Array ( [0] => orange [1] => orange ) // 2回目のマッチング:全体 + 第1グループ
    [2] => Array ( [0] => apple [1] => apple )   // 3回目のマッチング:全体 + 第1グループ
)
*/
// 例の文字列
$string = "apple orange banana apple";

// 正規表現パターン: 'apple' または 'orange' を検索し、括弧のグループで囲む
$pattern = '/(apple|orange)/';

// PREG_OFFSET_CAPTURE を使用
// - 各マッチング結果に文字列の開始位置(オフセット)を併せて格納
// - $matches : 結果を割り当てる配列変数
// - $matches の各要素は [一致した文字列, 開始位置インデックス] の形式
preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE);

// 結果の出力
print_r($matches);

/*
出力:
Array
(
    [0] => Array ( 
        [0] => Array("apple", 0)    // パターン全体 1回目のマッチングと位置
        [1] => Array("orange", 6)   // パターン全体 2回目のマッチングと位置
        [2] => Array("apple", 19)   // パターン全体 3回目のマッチングと位置
    )
    [1] => Array ( 
        [0] => Array("apple", 0)    // 第1グループ 1回目のマッチングと位置
        [1] => Array("orange", 6)   // 第1グループ 2回目のマッチングと位置
        [2] => Array("apple", 19)   // 第1グループ 3回目のマッチングと位置
    )
)
*/
// 例の文字列
$string = "apple banana";

// 正規表現パターン: 'apple', 'banana', 'orange' の3つのグループ
$pattern = '/(apple)|(banana)|(orange)/';

// PREG_UNMATCHED_AS_NULL を使用
// - 一致しなかったサブパターン(subpattern)を null として処理
// - $matches : 結果を割り当てる配列変数
// - $matches[0], $matches[1], ... 各グループのマッチングが null または文字列として格納される
preg_match_all($pattern, $string, $matches, PREG_PATTERN_ORDER | PREG_UNMATCHED_AS_NULL);

// 結果の出力
print_r($matches);

/*
出力:
Array
(
    [0] => Array ( [0] => apple [1] => banana )   // パターン全体のマッチング
    [1] => Array ( [0] => apple [1] => null )     // 第1グループ: 'apple' のみマッチ
    [2] => Array ( [0] => null [1] => banana )    // 第2グループ: 'banana' のみマッチ
    [3] => Array ( [0] => null [1] => null )      // 第3グループ: 'orange' のマッチングなし → null
)
*/
$string = 'お問い合わせは help@example.com または support@example.org までご連絡ください。';
$pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/';
$matches = array();

$result = preg_match_all($pattern, $string, $matches);

if ($result !== false && $result > 0) {
    print_r($matches[0]);       // すべてのマッチしたメールアドレスを出力
    echo "\n最初のメールアドレス: " . $matches[0][0]; // 最初のメールアドレスのみを出力
} else {
    echo 'メールアドレスの形式が見つかりません。';
}

// 出力:
// Array ( [0] => help@example.com [1] => support@example.org )
// 最初のメールアドレス: help@example.com
$string = '詳細は https://www.example.com で確認してください。';
$pattern = '/\b(?:https?:\/\/)?(?:www\.)?[a-zA-Z0-9-]+(?:\.[a-z]{2,})+(?:\/[^\s]*)?\b/';
$matches = array();

$result = preg_match_all($pattern, $string, $matches);

if ($result !== false && $result > 0) {
    echo $matches[0][0]; // 最初のマッチしたURLを出力
} else {
    echo 'URLの形式が見つかりません。';
}

// 出力: 'https://www.example.com'
$html = '<div class="container"><p>Hello, <b>world!</b></p></div>';
$tagPattern = '/<[^>]+>/';  // HTMLタグを検索する正規表現
$matches = array();

$result = preg_match_all($tagPattern, $html, $matches);

if ($result !== false && $result > 0) {
    print_r($matches[0]);  // すべてのマッチしたタグを出力
} else {
    echo 'タグが見つかりませんでした。';
}

// 出力:
// Array ( [0] => <div class="container"> [1] => <p> [2] => <b> [3] => </b> [4] => </p> [5] => </div> )
$css = '.header { color: #333; } .main-content { font-size: 16px; }';
$classPattern = '/\.([a-zA-Z0-9_-]+)/'; // CSSクラスセレクタのパターン
$matches = array();

$result = preg_match_all($classPattern, $css, $matches);

if ($result !== false && $result > 0) {
    print_r($matches[0]);  // マッチしたクラスセレクタ全体を出力
} else {
    echo 'CSSクラスセレクタが見つかりませんでした。';
}

// 出力:
// Array ( [0] => .header [1] => .main-content )