PHPackages                             hakre/pcre.php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. hakre/pcre.php

ActiveLibrary[CLI &amp; Console](/categories/cli)

hakre/pcre.php
==============

CLI utility for PCRE pattern search and replace through list of files (line based)

v0.1.0(5y ago)03AGPL-3.0-or-laterPHPPHP ^7.1CI failing

Since Nov 18Pushed 5y ago1 watchersCompare

[ Source](https://github.com/hakre/pcre.php)[ Packagist](https://packagist.org/packages/hakre/pcre.php)[ Docs](https://github.com/hakre/pcre.php)[ RSS](/packages/hakre-pcrephp/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (1)Versions (9)Used By (0)

pcre.php
========

[](#pcrephp)

PCRE pattern search and replace through list of files.

CLI wrapper around php preg\_grep / preg\_replace etc. taking a list of files from stdin/file to search and replace in (line based).

[![Latest Stable Version](https://camo.githubusercontent.com/3d46925c10a8a7a749230e0b1cf3d3b42e0535f9a2f16e52b88438119fd2bc88/68747470733a2f2f706f7365722e707567782e6f72672f68616b72652f706372652e7068702f762f737461626c65)](https://packagist.org/packages/hakre/pcre.php)[![Total Downloads](https://camo.githubusercontent.com/996252db6918d9a469d306eaf5fc1c16d5cbd9c3000adab6d1978d8bc902c198/68747470733a2f2f706f7365722e707567782e6f72672f68616b72652f706372652e7068702f646f776e6c6f616473)](https://packagist.org/packages/hakre/pcre.php)[![Latest Unstable Version](https://camo.githubusercontent.com/0a54776c2593e4396fd68dd06b1a44602af3e143900f35f1b0c58cab5282c7a7/68747470733a2f2f706f7365722e707567782e6f72672f68616b72652f706372652e7068702f762f756e737461626c65)](https://packagist.org/packages/hakre/pcre.php)[![License](https://camo.githubusercontent.com/7733d11e5cf5ac0a584272ca2120352e3f90eb5bb7d00860411ffcd4dc548eea/68747470733a2f2f706f7365722e707567782e6f72672f68616b72652f706372652e7068702f6c6963656e7365)](https://packagist.org/packages/hakre/pcre.php)[![composer.lock](https://camo.githubusercontent.com/bebef043b5eaafc881826a251bb58d7d5166b839eedb4d52672270b77d4fb4ad/68747470733a2f2f706f7365722e707567782e6f72672f68616b72652f706372652e7068702f636f6d706f7365726c6f636b)](https://packagist.org/packages/hakre/pcre.php)

[Usage](#usage) | [Examples](#examples) | [Installation](#installation) | [Development](#development)

---

Usage
-----

[](#usage)

```
usage: pcre.php [] [ []]
                [] --print-paths

Common options
    -n, --dry-run         do not write changes to files
    -v                    be verbose
    --show-match          show path, line number and match(es)/replacement(s)
    --count-matches       count distinct matches in descending order
    --print-paths         print paths to stdout, one by line and exit
    --lines-not | --lines-only
                          filter lines b/f doing search and replace
    -m, --multiple        search and replace multiple times on the same line

File selection options
    -T, --files-from=
                          read paths of files to operate on from . The
                          special filename "-" or given no filename will read
                          files from standard input
                          each filename is separated by LF ("\n") in a file
    --fnmatch    filter the list of path(s) by fnmatch() pattern
    --fnpcre     filter the list of path(s) by pcre pattern
    --only       only operate on files having a line matching pcre
                          pattern
    --invert              invert the meaning of --only pcre pattern match,
                          operate on files that do not have a line matching
                          the pcre pattern
    --file-match
                          only operate on files their contents (not lines)
                          matches the pcre pattern
    --file-match-invert   invert the meaning of --file-match

Operational options
    -C              run as if pcre.php was started in  instead
                          of the current working directory
    --version             display version information and exit

```

---

Examples
--------

[](#examples)

Most of these examples require to have the git utility installed.

Print a list of PHP file paths:

```
$ git ls-files '*.php' | pcre.php
pcre.php
matches in 0 out of 1 files (0.0%)

```

Search a list of files:

```
$ git ls-files '*.php' | pcre.php '/getopt/'
pcre.php
matches in 1 out of 1 files (100.0%)

```

And actually show each match:

```
$ git ls-files '*.php' | pcre.php --show-match '/getopt/'
  pcre.php
    352:  * Class getopt
    354:  * static helper class for parsing command-line arguments w/ php getopt()
    356:  * $opts    - array in the form of getopt() return
    359: class getopt
    364:      * @param array $opts getopt result
    386:      * (hint: maybe using `getopt::arg(...) ?? $default` is more applicable)
    416:      * index getopt() options and longoptions
    453:      * @param int $optind getopt parse stop
    456:      * @see getopt::erropt_msg()
    458:     public static function erropt(string $options, array $longopts, int $optind, $handler = ['getopt', 'erropt_msg']): bool
    460:         $idxopt = getopt::idxopt($options, $longopts);
    466:             if ($index >= $optind) break;  // stop at stop (halt of getopt() option parsing)
    516:      * standard error message callback for @see getopt::erropt() handler
    570: $opts = getopt($opt[0], $opt[1], $optind);
    571: if (getopt::erropt($opt[0], $opt[1], $optind)) {
    575: $opts['verbose'] = getopt::arg(getopt::args($opts, 'v'), true, false);
    577: $input = getopt::arg(getopt::args($opts, 'T', 'files-from'), '-', '-');
    590: $multiple = getopt::arg(getopt::args($opts, 'm', 'multiple'));
  matches in 1 out of 1 files (100.0%)

```

Replace matches:

```
$ git ls-files '*.php' | pcre.php -n '/getopt/' 'replace_getopt'
...

```

To revert checkout/reset with git. Alternatively use dry-run first and preview changes with show each match (which is extended with the replace):

```
$ git ls-files *.php | pcre.php --dry-run --show-match '/getopt/' 'replace_getopt'
...

```

### More Examples

[](#more-examples)

```
$ pcre.php --help

```

show usage information

---

Installation
------------

[](#installation)

*Next:* [Install Latest Stable `pcre.php` from Github](#install-latest-stable-pcrephp-from-github)

Have a PHP 7.1+ PHP binary as `/usr/bin/env` on the system.

Make `pcre.php` executable (git has you covered on checkout) and have it within your path.

For example, after cloning and considering `~/bin` is a directory within your `$PATH`:

```
$ cp -a pcre.php ~/bin

```

or alternatively create a symbolic link (symlink) for using the source version:

```
$ ln -sT "$(realpath ./pcre.php)" ~/bin/pcre.php

```

Then invoking:

```
$ pcre.php

```

should just show the cursor blinking. Signal `eof` (ctrl+d / Unicode END\_OF\_TRANSMISSION) to get a result:

```
matches in 0 out of 0 files

```

Congratulations, you managed to search no files for nothing!

This confirms installation works.

### Install Latest Stable `pcre.php` from Github

[](#install-latest-stable-pcrephp-from-github)

Obtaining the latest stable archive from Github and extracting to `~/bin/pcre.php` exemplary works with the following command:

```
$ unlink ~/bin/pcre.php \
  ; wget -O - https://github.com/hakre/pcre.php/archive/master.tar.gz \
  | tar -xzf- -C ~/bin --strip-components=1 pcre.php-master/pcre.php \
  && pcre.php --version

```

which will unlink a potentially existing symbolic link at `~/bin/pcre.php`, then fetch the git archive of current HEAD of master (which is considered the most stable at the level of most features) and extracting `pcre.php` to `~/bin` (owned by the current user/group, executable bits set) and then outputting the version (you should see a tagged version like `pcre.php v0.0.7`.

Instead of Github, also any tarball from `git-archive` of the project, remote or local, can be used.

Take note as this is directly installing from the internet, that if the repository has been hijacked, you might not obtain the software you think you obtain - just as a word of caution.

If a command similar like in the example above is used to overwrite `pcre.php` inside a git checkout of the project itself, git will show the file as modified but `git-diff` might not show changes due to `pcre.php ident` in `.gitattributes` which is used to differ between concrete version numbers and the source revision (`$ pcre.php --version`).

Development
-----------

[](#development)

This project is merely scratching an itch for me however as I need to develop it myself, there is a certain baseline:

- *Git required*. Yet no specific version requirements known
    - It works from source, so `git clone` is a valid way to obtain the utility.
- *Composer required*. It comes with a build and also with tests:
    - `$ composer build` - invokes the build (script)
    - `$ composer test` - runs just the tests (no full build) the tests are smoke tests and the Phpunit test suite right now (these were not available a couple of days ago).
    - `$ composer package` - can you package it. runs the whole build and then packages under the current revision.

Right now `pcre.php` is a single PHP file. So patching, maintaining and even developing requires some working into. The benefit is that most things are directly accessible. The downside is that things might change abruptly.

Regressions can be easily tested for by adding a test-case to `smoke.sh` and perhaps extending the fixture (in `tests/fixture`) .

Any units (functions, classes) can be unit-tested with Phpunit in a recent version, it is pre-configured in the repository and can be installed with composer as a development requirement.

The build script so far takes the usage instructions out of the `pcre.php` file into `README.md` so that it is kept up to date.

Feature requests are best done with a pull-request to demonstrate the feature as thought of. It's fine if it destroys some other functionality as long as this is properly highlighted in a pull- request, so yes, this is a project where you can file pseudo code pull requests even.

### Package

[](#package)

A package (at a revision) can be produced with the `git archive`command:

```
$ git archive -o hakre-pcre.php.tar.gz HEAD

```

The package will be generated in the project root then which git will highlight as new files.

Existing files are being overwritten but files should be reproducible per the revision.

The full packaging with a version identifier in the output file- name can be done (running test, build etc. first) with:

```
$ composer package

```

To test manually and check the list of files in the package:

```
$ git archive HEAD . | tar -t
README.md
pcre.php

```

I think it's fine to have the read-me and the actual utility packaged but skip the rest. This is also tested for.

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~37 days

Recently: every ~65 days

Total

8

Last Release

2110d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/378849?v=4)[hakre](/maintainers/hakre)[@hakre](https://github.com/hakre)

---

Top Contributors

[![hakre](https://avatars.githubusercontent.com/u/378849?v=4)](https://github.com/hakre "hakre (47 commits)")

---

Tags

searchcliutilityshellfilesregexPCREgitreplace

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hakre-pcrephp/health.svg)

```
[![Health](https://phpackages.com/badges/hakre-pcrephp/health.svg)](https://phpackages.com/packages/hakre-pcrephp)
```

###  Alternatives

[kevinlebrun/colors.php

Colors for PHP CLI scripts

3426.7M45](/packages/kevinlebrun-colorsphp)[projektgopher/whisky

A simple CLI tool for managing a project's git hooks across multiple members.

241281.3k20](/packages/projektgopher-whisky)[misterion/ko-process

Simple pcntl fork wrapper and process manager

177337.7k6](/packages/misterion-ko-process)[guanguans/ai-commit

Automagically generate conventional git commit message with AI. - 使用 AI 自动生成约定式 git 提交信息。

39231.2k10](/packages/guanguans-ai-commit)[yoeunes/regex-parser

A powerful PCRE regex parser with lexer, AST builder, validation, ReDoS analysis, and syntax highlighting. Zero dependencies, blazing fast, and production-ready.

2946.4k5](/packages/yoeunes-regex-parser)[alecrabbit/php-console-spinner

Extremely flexible spinner for \[async\] php cli applications

24032.0k2](/packages/alecrabbit-php-console-spinner)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
