PHP Version
4+
/* Finds all substrings that match
   regular expression patterns in a string
   and returns the count as an integer. */

// The given string
$str = 'apple orange banana apple';

// The regular expression pattern to search for
$pattern = '/apple/';

// Apply the function and return the result
$result = preg_match_all($pattern, $str);
var_dump($result); // Output: int(2)

/* All matched substrings can also be stored
   in an array composed of separate elements.
   Specify a variable to assign to the third parameter. */

// Specify a variable ($matches) to assign to the third parameter
$result = preg_match_all($pattern, $str, $matches);

// Check the assigned array
print_r($matches);
/* Output:
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,
    the string to be searched[,
    an array to store the matched parts[,
    flags that specify additional options[,
    the offset within the string where the search starts]]]
);
*/
$pattern = '/apple/';
$subject = "apple orange banana apple";

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

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

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

if ($result !== false) {
    var_dump($result); // Output: int(0)
}
error_reporting(0); // Disable error output

$pattern = '/apple'; // Invalid regular expression
$subject = "apple orange banana apple";

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

if ($result !== false) {
    var_dump($result);
} else {
    echo 'An error occurred.';
}

// Output: 'An error occurred.'
// Find numbers in the given string
$string = 'This is a sample text containing 123 and 456.';

// Pattern: match numbers
$pattern = '/\d+/';

// Declare an empty array to store the matches
$matches = array();

// Pass $matches as a parameter
$num_of_matches = preg_match_all($pattern, $string, $matches);

// Output the result
if ($num_of_matches > 0) {
    var_dump($matches);
    // array(1) {
    //   [0]=> array(2) {
    //     [0]=> string(3) "123"
    //     [1]=> string(3) "456"
    //   }
    // }
}
// Example string
$string = "apple orange banana apple";

// Regular expression pattern:
// Find 'apple' or 'orange' and wrap it in a parenthesized group
$pattern = '/(apple|orange)/';

// Using PREG_PATTERN_ORDER
// - Organizes the matching results by pattern (capture group)
// - $matches    : the array variable to which the results are assigned
// - $matches[0] : substrings that match the entire pattern
// - $matches[1] : substrings that match the first parenthesized group
preg_match_all($pattern, $string, $matches, PREG_PATTERN_ORDER);

// Output the result
print_r($matches);

/*
Output:
Array
(
    [0] => Array ( [0] => apple [1] => orange [2] => apple )
    [1] => Array ( [0] => apple [1] => orange [2] => apple )
)
*/
// Example string
$string = "apple orange banana apple";

// Regular expression pattern:
// Match 'apple' or 'orange' and wrap it in a capture group
$pattern = '/(apple|orange)/';

// Use PREG_SET_ORDER
// - Groups results by individual match
// - $matches                      : the array variable to which the results are assigned
// - $matches[0], $matches[1], ... : each represents one match
preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);

// Output the result
print_r($matches);

/*
Output:
Array
(
    [0] => Array ( [0] => apple [1] => apple )   // First match: entire match + first group
    [1] => Array ( [0] => orange [1] => orange ) // Second match: entire match + first group
    [2] => Array ( [0] => apple [1] => apple )   // Third match: entire match + first group
)
*/
// Example string
$string = "apple orange banana apple";

// Regular expression pattern:
// Match 'apple' or 'orange' and wrap it in a capture group
$pattern = '/(apple|orange)/';

// Use PREG_OFFSET_CAPTURE
// - Stores the starting offset along with each matched string
// - $matches : the array variable to which the results are assigned
// - Each element is in the form [matched string, starting offset]
preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE);

// Output the result
print_r($matches);

/*
Output:
Array
(
    [0] => Array ( 
        [0] => Array("apple", 0)    // First full match and its offset
        [1] => Array("orange", 6)   // Second full match and its offset
        [2] => Array("apple", 19)   // Third full match and its offset
    )
    [1] => Array ( 
        [0] => Array("apple", 0)    // First group match and its offset
        [1] => Array("orange", 6)   // Second group match and its offset
        [2] => Array("apple", 19)   // Third group match and its offset
    )
)
*/
// Example string
$string = "apple banana";

// Regular expression pattern:
// Three groups: 'apple', 'banana', 'orange'
$pattern = '/(apple)|(banana)|(orange)/';

// Use PREG_UNMATCHED_AS_NULL
// - Treats unmatched subpatterns as null
// - $matches : the array variable to which the results are assigned
// - Each group match is stored as either a string or null
preg_match_all(
    $pattern,
    $string,
    $matches,
    PREG_PATTERN_ORDER | PREG_UNMATCHED_AS_NULL
);

// Output the result
print_r($matches);

/*
Output:
Array
(
    [0] => Array ( [0] => apple [1] => banana )   // Full pattern matches
    [1] => Array ( [0] => apple [1] => null )     // First group: only 'apple' matched
    [2] => Array ( [0] => null [1] => banana )    // Second group: only 'banana' matched
    [3] => Array ( [0] => null [1] => null )      // Third group: no match → null
)
*/
$string = 'For inquiries, please contact help@example.com or 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]); // Output all matched email addresses
    echo "\nFirst email: " . $matches[0][0]; // Output only the first email address
} else {
    echo 'No valid email address format was found.';
}

// Output:
// Array ( [0] => help@example.com [1] => support@example.org )
// First email: help@example.com
$string = 'For more information, visit 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]; // Output the first matched URL
} else {
    echo 'No valid URL format was found.';
}

// Output: https://www.example.com
$html = '<div class="container"><p>Hello, <b>world!</b></p></div>';
$tagPattern = '/<[^>]+>/'; // Regular expression to find HTML tags
$matches = array();

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

if ($result !== false && $result > 0) {
    print_r($matches[0]); // Output all matched tags
} else {
    echo 'No tags were found.';
}

// Output:
// 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_-]+)/'; // Pattern for CSS class selectors
$matches = array();

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

if ($result !== false && $result > 0) {
    print_r($matches[0]); // Output all matched class selectors
} else {
    echo 'No CSS class selectors were found.';
}

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