PHPackages                             hashbang/pherl - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. hashbang/pherl

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hashbang/pherl
==============

Perl like functionality for PHP

v1.0.0(12y ago)228MITPHPPHP &gt;=5.0.0

Since Mar 21Pushed 12y ago1 watchersCompare

[ Source](https://github.com/hash-bang/Pherl)[ Packagist](https://packagist.org/packages/hashbang/pherl)[ Docs](https://github.com/hash-bang/Pherl)[ RSS](/packages/hashbang-pherl/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

Pherl - Perl like functionality for PHP
=======================================

[](#pherl---perl-like-functionality-for-php)

This module is intended to provide simple Perl like functionality for PHP users.

It contains a number of convenience functions cheerfully ripped from the Perl programming languages which make coding a lot easier for the terminally impatient.

- **QW** - Quick array initalization via simple strings
- **KEYVAL** - Extract a key=&gt;val relationship from an array-of-arrays
- **VALUES** - Extract a value from a hash array and return it as an indexed array
- **PICK** - Randomly pick elements from an array
- **ENCASE** - Quickly add a prefix / suffix to an array of strings
- **EVALSTR** - Process a string similar to how PHP expands variables
- **RE** - Perl like regular expression syntax for PHP

Installation
============

[](#installation)

Installing into CodeIgniter / CakePHP etc.
------------------------------------------

[](#installing-into-codeigniter--cakephp-etc)

Download this GIT repository and copy into your application directory.

Alternatively, install with [Composer](http://getcomposer.org).

Since Composer doesn't seem to like just loading files full of helper functions you will need to place:

```
include('vendor/hashbang/pherl/lib/pherl.php');

```

Early in your load order to use the provided Pherl functions.

`qw` - Quick array initalizer
=============================

[](#qw---quick-array-initalizer)

Quickly initalize arrays by providing a single string. The array elements are determined by any whitespace.

```
// Create an array with three elements (foo, bar and baz)
$array = qw('foo bar baz');

$array = qw('foo    bar    baz');

$array = qw('
	foo
	bar
	baz
');

```

`keyval` - Create a key/val relationship from an array-of-arrays
================================================================

[](#keyval---create-a-keyval-relationship-from-an-array-of-arrays)

Quickly reorder arrays by picking the key and value arrangement from an array of arrays

```
$in = array(
	array(
		'name' => 'Earl',
		'age' => 35,
	),
	array(
		'name' => 'John',
		'age' => 24,
	),
);

$a = keyval('name', 'age', $in); // $a is now array('Earl' => 35, 'John' => 24)

```

Specifying 'OFFSET' as the key uses the offset of the index within the array-of-arrays.

`values` - Extract a key from a hash and return as indexed array
================================================================

[](#values---extract-a-key-from-a-hash-and-return-as-indexed-array)

Quickly reorder arrays by picking the key and value arrangement from an array of arrays

```
$in = array(
	array(
		'name' => 'Earl',
		'age' => 35,
	),
	array(
		'name' => 'John',
		'age' => 24,
	),
);

$a = values('name', $in); // $a is now array('Earl', 'John')

```

`values` is actually just a shortcut function for `keyval(null, $value, $array)`.

`pick` - Pick random elements from an array
===========================================

[](#pick---pick-random-elements-from-an-array)

Choose a single random element from an array.

```
$item = pick(qw('foo bar baz')); // Chooses either foo, bar or baz

```

`encase` - Add a prefix / suffix to an array of strings
=======================================================

[](#encase---add-a-prefix--suffix-to-an-array-of-strings)

The `encase()` function allows you to quickly enclose each string in an array with a given prefix and suffix.

```
$tags = encase(qw('a img hr', '')); // Returns: , ,

```

The meta string 'OFFSET' will be replaced with the key of the array being iterated over.

`evalstr` - Expand PHP style strings
====================================

[](#evalstr---expand-php-style-strings)

Return the computed result of a string using local variables.

```
echo evalstr('Hello $name', array('name' => 'Matt')); // Returns Matt

echo evalstr('Hello {$user['name']}', array('user' => $this->GetAUser(123))); // Returns the 'user' objects 'GetAUser' methods 'name' property

```

`re` - Perl like Regular Expressions
====================================

[](#re---perl-like-regular-expressions)

The Re() function provides Regular Expression functionality in a Perl like way.

Simple matching
---------------

[](#simple-matching)

Determine if the string 'needle' exists in $haystack:

```
if (re('/needle/', $haystack)) {
	// Do something
}

```

Simple extraction
-----------------

[](#simple-extraction)

Extract a match from an input string

```
$haystack = 'foo bar baz quz quuz';
list($word) = re('/(qu.)/', $haystack);
echo $word; // Output: 'quz'

```

RE can also return only the first captured element by using the '1' modifier. The following code will act the same as the above but force the only match into a string rather than an array:

```
$haystack = 'foo bar baz quz quuz';
$word = re('/(qu.)/1', $haystack);
echo $word; // Output: 'quz'

```

Multiple extraction into an array
---------------------------------

[](#multiple-extraction-into-an-array)

Extract multiple matches into an array

```
$haystack = 'foo bar baz quz quuz';
$words = re('/(ba.)/', $haystack);
print_r($words); // Output: array('bar', 'baz')

```

This is the same syntax as Simple Extraction. When multiple elements are found RE will return the elements as an array automatically.

Multiple extraction into variables
----------------------------------

[](#multiple-extraction-into-variables)

You can use PHP's list() function to automatically cram the output of RE into a series of variables.

```
$haystack = 'Matt is 28 years old';
list($name, $age) = re('/^(.+?) is ([0-9]+) years old$/', $haystack);

```

Simple substitution and replacement
-----------------------------------

[](#simple-substitution-and-replacement)

Substitution (also known as replacement) is also supported.

```
$haystack = 'foo bar baz foo bar baz';
$output = re('s/bar/BAR/', $haystack);
echo $output; // Output: 'foo BAR baz foo bar baz'

```

By default only the first matching element is replaced. If you want to replace all matching items use the 'g' modifier:

```
$haystack = 'foo bar baz foo bar baz';
$output = re('s/bar/BAR/', $haystack);
echo $output; // Output: 'foo BAR baz foo BAR baz'

```

Substitution with back-references
---------------------------------

[](#substitution-with-back-references)

Replace the words 'bar' and 'baz' into 'FOUND-r' and 'FOUND-z':

```
$haystack = 'foo bar baz';
$output = re('s/(ba.)/FOUND-\1/', $haystack);
echo $output; // Output: 'foo FOUND-bar FOUND-baz'

```

\\1 and onwards is automatically set to the captured item.

Translation
-----------

[](#translation)

Although not really used that much you can replace single characters based on a range:

```
$haystack = 'foo bar baz';
$output = re('tr/a-z/A-Z');
echo $output; // Output: 'FOO BAR BAZ'

```

Perl to PHP reference
---------------------

[](#perl-to-php-reference)

This section contains some commonly used Perl syntax and the PHP equivelent when using this module. This is included because sometimes examples are more helpful than API waffle.

  Example Perl PHP + Pherl   Extraction ```
$_ = 'foo bar baz';
($one, $two, $three) =~ m/(.*) .{3} .../;
```

  ```
$haystack = 'foo bar baz';
list($one, $two, $three) = re('m/(.*) .{3} .../', $haystack);
```

    Subsitution ```
$_ = 'foo bar baz';
$new = s/foo/QUZ/;
```

  ```
$haystack = 'foo bar baz';
$new = re('s/foo/QUZ/', $haystack);
```

    Translation ```
$_ = 'foo bar baz';
$new = tr/a-z/A-Z/;
```

  ```
$haystack = 'foo bar baz';
$new = re('tr/a-z/A-Z/', $haystack);
```

  TODO
====

[](#todo)

- Translation (tr//) not working correctly
- Support for callback functions for substitutions

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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

Unknown

Total

1

Last Release

4438d ago

### Community

Maintainers

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

---

Top Contributors

[![hash-bang](https://avatars.githubusercontent.com/u/624527?v=4)](https://github.com/hash-bang "hash-bang (29 commits)")

---

Tags

functionsarrayshashesperl

### Embed Badge

![Health badge](/badges/hashbang-pherl/health.svg)

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

###  Alternatives

[ilya/belt

A handful of tools for PHP developers.

71020.8k1](/packages/ilya-belt)[maciejczyzewski/bottomline

A full-on PHP manipulation utility belt that provides support for working with arrays, objects, and iterables; a lodash or underscore equivalent for PHP.

477631.4k10](/packages/maciejczyzewski-bottomline)[illuminated/helper-functions

Laravel-specific and pure PHP Helper Functions.

107586.6k7](/packages/illuminated-helper-functions)[minwork/array

Pack of advanced array functions specifically tailored for: associative (assoc) array, multidimensional array, array of objects and handling nested array elements

66256.1k5](/packages/minwork-array)[jawira/plantuml-encoding

PlantUML encoding functions

20561.9k9](/packages/jawira-plantuml-encoding)[me-io/php-lodash

A full-on PHP manipulation utility-belt that provides support for the usual functional.

38100.0k](/packages/me-io-php-lodash)

PHPackages © 2026

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