composer/pcre
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The Composer PCRE library is a PHP package that provides a type-safe wrapper for preg_*
functions. This means that it ensures these functions do not fail silently, avoiding unexpected nulls
that may not be handled correctly. It simplifies the possible return values from all the preg_*
functions, which are usually filled with various edge cases. This makes the library particularly useful when working with static analysis tools like PHPStan or Psalm. This library is ideal if you're working with regular expressions in PHP and looking for a way to handle exceptions and unexpected return values more gracefully.
To utilize the Composer PCRE library, you need to include it in your project using Composer. In your terminal, execute the command:
$ composer require composer/pcre
Once the package is installed, you can use its various functionalities in your PHP script. For example, instead of using preg_match()
or preg_match_all()
, you can use Preg::match()
or Preg::matchAll()
respectively from the Preg
class provided by the library.
Here is how to use these methods:
use Composer\Pcre\Preg;
if (Preg::match('{fo+}', $string, $matches)) { /*...*/ }
if (Preg::matchWithOffsets('{fo+}', $string, $matches)) { /*...*/ }
$newString = Preg::replace('{fo+}', 'bar', $string);
The library also provides match methods that return bool
rather than int
, for stricter type safety. And if you need stricter type safety for match groups, the library provides *StrictGroups
method variants.
The documentation for the Composer PCRE library is embedded within its GitHub repository. Detailed explanations of the library's functionalities, including its methods, return types, installation process, requirements, and usage examples can be found on its GitHub page at https://github.com/composer/pcre.git. Particular attention should be paid to the 'Basic Usage' and 'Restrictions / Limitations' sections for comprehensive guidance on how to effectively utilize the library. Also, the GitHub repository contains the source code for the project, which may prove useful for those who wish to gain deeper insights into the workings of the library.