PHP code example of hoels / regex

1. Go to this page and download the library: Download hoels/regex library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hoels / regex example snippets


Regex::containsMatchIn(string $regex, string $input): bool

Regex::find(string $regex, string $input): MatchResult|null

Regex::findAll(string $regex, string $input): MatchResult[]

Regex::matchAt(string $regex, string $input, int $index): MatchResult|null

Regex::replace(string $regex, string $input, string $replacement): string

echo Regex::replace("/(\\d\\.\\d)\\.\\d+/", "We support PHP 8.1.26 and 8.2.13.", "$1"); // We support PHP 8.1 and 8.2.

Regex::replaceFirst(string $regex, string $input, string $replacement): string

echo Regex::replaceFirst("/(\\d\\.\\d)\\.\\d+/", "We support PHP 8.1.26 and 8.2.13.", "$1"); // We support PHP 8.1 and 8.2.3.

use Regex\Regex;

if (Regex::containsMatchIn(pattern: "/^[a-z]+$/", subject: $_GET["input"])) {
    ...
}

use Regex\Regex;

$matchResult = Regex::find(pattern: "/(?P<year>\d{4})\/(?P<month>\d{2})\/(?P<day>\d{2})/", subject: $input);
if ($matchResult !== null) {
    $year = $matchResult->getGroup("year")?->getValue();
    $month = $matchResult->getGroup("month")?->getValue();
    $day = $matchResult->getGroup("day")?->getValue();
}