PHPackages                             permafrost-dev/php-code-search - 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. [Search &amp; Filtering](/categories/search)
4. /
5. permafrost-dev/php-code-search

ActiveLibrary[Search &amp; Filtering](/categories/search)

permafrost-dev/php-code-search
==============================

Search PHP code for function &amp; method calls, variable assignments, and more

1.12.0(1y ago)28424.0k↓73%[1 PRs](https://github.com/permafrost-dev/php-code-search/pulls)2MITPHPPHP ^7.4|^8.0

Since Jul 4Pushed 1y ago2 watchersCompare

[ Source](https://github.com/permafrost-dev/php-code-search)[ Packagist](https://packagist.org/packages/permafrost-dev/php-code-search)[ Docs](https://github.com/permafrost-dev/php-code-search)[ GitHub Sponsors](https://github.com/sponsors/permafrost-dev)[ RSS](/packages/permafrost-dev-php-code-search/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (10)Dependencies (4)Versions (35)Used By (2)

Searching PHP source code made easy
===================================

[](#searching-php-source-code-made-easy)

 [![php-code-search logo](https://camo.githubusercontent.com/b9be4b6989bd720e00d32c6efbf007466d878d64df5259198662c33d70d213ad/68747470733a2f2f7374617469632e7065726d6166726f73742e6465762f696d616765732f7068702d636f64652d7365617263682f7068702d636f64652d7365617263682d6c6f676f2e706e67)](https://camo.githubusercontent.com/b9be4b6989bd720e00d32c6efbf007466d878d64df5259198662c33d70d213ad/68747470733a2f2f7374617469632e7065726d6166726f73742e6465762f696d616765732f7068702d636f64652d7365617263682f7068702d636f64652d7365617263682d6c6f676f2e706e67)

 [![Package Version](https://camo.githubusercontent.com/d5bdd6702b70919f83807f353dbd0f555f85bc973013aa58faddb272af338826/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f7065726d6166726f73742d6465762f7068702d636f64652d7365617263682e7376673f736f72743d73656d766572266c6f676f3d67697468756226)](https://camo.githubusercontent.com/d5bdd6702b70919f83807f353dbd0f555f85bc973013aa58faddb272af338826/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f7065726d6166726f73742d6465762f7068702d636f64652d7365617263682e7376673f736f72743d73656d766572266c6f676f3d67697468756226) [![Downloads](https://camo.githubusercontent.com/69e8ad51e53dbe67df008cf1567281947c51d108a0b0b2167ceae81f17bde25d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7065726d6166726f73742d6465762f7068702d636f64652d7365617263683f6c6f676f3d7061636b6167697374266c6f676f436f6c6f723d253233666666)](https://camo.githubusercontent.com/69e8ad51e53dbe67df008cf1567281947c51d108a0b0b2167ceae81f17bde25d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7065726d6166726f73742d6465762f7068702d636f64652d7365617263683f6c6f676f3d7061636b6167697374266c6f676f436f6c6f723d253233666666) [![license](https://camo.githubusercontent.com/b2e576c7f520b9d34ae5cc43cf177fdb68597a12be31225a4aefc715c495f8cc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7065726d6166726f73742d6465762f7068702d636f64652d7365617263682e7376673f6c6f676f3d6f70656e736f75726365696e697469617469766526)](https://camo.githubusercontent.com/b2e576c7f520b9d34ae5cc43cf177fdb68597a12be31225a4aefc715c495f8cc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7065726d6166726f73742d6465762f7068702d636f64652d7365617263682e7376673f6c6f676f3d6f70656e736f75726365696e697469617469766526) [![Test Run Status](https://github.com/permafrost-dev/php-code-search/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/permafrost-dev/php-code-search/actions/workflows/run-tests.yml/badge.svg?branch=main) [![code coverage](https://camo.githubusercontent.com/6f757505d843449d8149f5ca5085190b4c1d31917a8f1f65c810f35d1f145579/68747470733a2f2f636f6465636f762e696f2f67682f7065726d6166726f73742d6465762f7068702d636f64652d7365617263682f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://camo.githubusercontent.com/6f757505d843449d8149f5ca5085190b4c1d31917a8f1f65c810f35d1f145579/68747470733a2f2f636f6465636f762e696f2f67682f7065726d6166726f73742d6465762f7068702d636f64652d7365617263682f6272616e63682f6d61696e2f67726170682f62616467652e737667)

---

Search PHP source code for function &amp; method calls, variable assignments, classes and more directly from PHP.

---

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

[](#installation)

```
composer require permafrost-dev/php-code-search
```

Searching
---------

[](#searching)

To search a file, use the `search` method. Its only parameter may be either a string containing a valid filename or an instance of `\Permafrost\PhpCodeSearch\Support\File`.

To search a string instead, use the `searchCode` method.

The search methods return an instance of `Permafrost\PhpCodeSearch\Results\FileSearchResults`, which has a `results` property.

Each `result` is an instance of `Permafrost\PhpCodeSearch\Results\SearchResult` with the following properties:

- `node` - the specific item that was found
    - `node->name(): string`
- `location` - the location in the file that the item was found
    - `location->startLine(): int`
    - `location->endLine(): int`
- `snippet` - a snippet of code lines from the file with the result line in the middle
    - `snippet->toString(): string`
- `file()` *(method)* - provides access to the file that was searched

### Searching

[](#searching-1)

To search through the code in a string or file, use the `Searcher` class:

```
use Permafrost\PhpCodeSearch\Searcher;

$searcher = new Searcher();
```

To search a file, use the `search` method, and the `searchCode` method to search a string of code.

```
$searcher
    ->functions(['strtolower', 'strtoupper'])
    ->search('./file1.php');

$searcher
    ->variables(['/^one[A-Z]$/'])
    ->searchCode('
