Download the PHP package composer/pcre without Composer

On this page you can find all versions of the php package composer/pcre. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package pcre

composer/pcre

PCRE wrapping library that offers type-safe preg_* replacements.

This library gives you a way to ensure preg_* functions do not fail silently, returning unexpected nulls that may not be handled.

As of 3.0 this library enforces PREG_UNMATCHED_AS_NULL usage for all matching and replaceCallback functions, read more below to understand the implications.

It thus makes it easier to work with static analysis tools like PHPStan or Psalm as it simplifies and reduces the possible return values from all the preg_* functions which are quite packed with edge cases. As of v2.2.0 / v3.2.0 the library also comes with a PHPStan extension for parsing regular expressions and giving you even better output types.

This library is a thin wrapper around preg_* functions with some limitations. If you are looking for a richer API to handle regular expressions have a look at rawr/t-regx instead.

Continuous Integration

Installation

Install the latest version with:

Requirements

Basic usage

Instead of:

You can now call these on the Preg class:

The main difference is if anything fails to match/replace/.., it will throw a Composer\Pcre\PcreException instead of returning null (or false in some cases), so you can now use the return values safely relying on the fact that they can only be strings (for replace), ints (for match) or arrays (for grep/split).

Additionally the Preg class provides match methods that return bool rather than int, for stricter type safety when the number of pattern matches is not useful:

Finally the Preg class provides a few *StrictGroups method variants that ensure match groups are always present and thus non-nullable, making it easier to write type-safe code:

Note: This is generally safe to use as long as you do not have optional subpatterns (i.e. (something)? or (something)* or branches with a | that result in some groups not being matched at all). A subpattern that can match an empty string like (.*) is not optional, it will be present as an empty string in the matches. A non-matching subpattern, even if optional like (?:foo)? will anyway not be present in matches so it is also not a problem to use these with *StrictGroups methods.

If you would prefer a slightly more verbose usage, replacing by-ref arguments by result objects, you can use the Regex class:

Note that preg_grep and preg_split are only callable via the Preg class as they do not have complex return types warranting a specific result object.

See the MatchAllResult, ReplaceResult class sources for more details.

Restrictions / Limitations

Due to type safety requirements a few restrictions are in place.

PREG_UNMATCHED_AS_NULL

As of 2.0, this library always uses PREG_UNMATCHED_AS_NULL for all match* and isMatch* functions. As of 3.0 it is also done for replaceCallback and replaceCallbackArray.

This means your matches will always contain all matching groups, either as null if unmatched or as string if it matched.

The advantages in clarity and predictability are clearer if you compare the two outputs of running this with and without PREG_UNMATCHED_AS_NULL in $flags:

no flag PREG_UNMATCHED_AS_NULL
array (size=4) array (size=5)
0 => string 'ac' (length=2) 0 => string 'ac' (length=2)
1 => string 'a' (length=1) 1 => string 'a' (length=1)
2 => string '' (length=0) 2 => null
3 => string 'c' (length=1) 3 => string 'c' (length=1)
4 => null
group 2 (any unmatched group preceding one that matched) is set to ''. You cannot tell if it matched an empty string or did not match at all group 2 is null when unmatched and a string if it matched, easy to check for
group 4 (any optional group without a matching one following) is missing altogether. So you have to check with isset(), but really you want isset($m[4]) && $m[4] !== '' for safety unless you are very careful to check that a non-optional group follows it group 4 is always set, and null in this case as there was no match, easy to check for with $m[4] !== null

PHPStan Extension

To use the PHPStan extension if you do not use phpstan/extension-installer you can include vendor/composer/pcre/extension.neon in your PHPStan config.

The extension provides much better type information for $matches as well as regex validation where possible.

License

composer/pcre is licensed under the MIT License, see the LICENSE file for details.


All versions of pcre with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4 || ^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package composer/pcre contains the following files

Loading the files please wait ....