PHPackages                             traderinteractive/filter-strings - 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. [Mail &amp; Notifications](/categories/mail)
4. /
5. traderinteractive/filter-strings

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

traderinteractive/filter-strings
================================

A filtering implementation for verifying strings

v4.2.0(1y ago)1102.7k↓32.2%4[1 issues](https://github.com/traderinteractive/filter-strings-php/issues)2MITPHPPHP ^7.3 || ^8.0CI passing

Since Mar 6Pushed 1y ago7 watchersCompare

[ Source](https://github.com/traderinteractive/filter-strings-php)[ Packagist](https://packagist.org/packages/traderinteractive/filter-strings)[ RSS](/packages/traderinteractive-filter-strings/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (16)Used By (2)

filter-strings-php
==================

[](#filter-strings-php)

[![Build Status](https://camo.githubusercontent.com/d52c7409241481e47477e45482ed891fb826a0212a9aeb7f50030ab5f15d4214/68747470733a2f2f7472617669732d63692e6f72672f747261646572696e7465726163746976652f66696c7465722d737472696e67732d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/traderinteractive/filter-strings-php)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/124ee96b12a85b821a7b873fdf7ef85fbcde807f091766dcefb0278074aed2e2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f747261646572696e7465726163746976652f66696c7465722d737472696e67732d7068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/traderinteractive/filter-strings-php/?branch=master)[![Coverage Status](https://camo.githubusercontent.com/fc49f9a88b832962a464a4d075de1518ae240eab44684fb9c75edc39388cd7a0/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f747261646572696e7465726163746976652f66696c7465722d737472696e67732d7068702f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/traderinteractive/filter-strings-php?branch=master)

[![Latest Stable Version](https://camo.githubusercontent.com/2d262ba148d83cf3cd39b9c103c179361c38422c9c2d722bd0aee5493f86d5cc/68747470733a2f2f706f7365722e707567782e6f72672f747261646572696e7465726163746976652f66696c7465722d737472696e67732f762f737461626c65)](https://packagist.org/packages/traderinteractive/filter-strings)[![Latest Unstable Version](https://camo.githubusercontent.com/421a2ca834c2760bc34979125da2715bf415d4b51a4fd18b33b914a0c1a8cb9a/68747470733a2f2f706f7365722e707567782e6f72672f747261646572696e7465726163746976652f66696c7465722d737472696e67732f762f756e737461626c65)](https://packagist.org/packages/traderinteractive/filter-strings)[![License](https://camo.githubusercontent.com/ddd9ce3e6361fc673f5bc08c0b856e56aa36b5f9fe1f95cb9c4949e76ad24d34/68747470733a2f2f706f7365722e707567782e6f72672f747261646572696e7465726163746976652f66696c7465722d737472696e67732f6c6963656e7365)](https://packagist.org/packages/traderinteractive/filter-strings)

[![Total Downloads](https://camo.githubusercontent.com/7145e5876f712fd395716c039d7dcf3d83701ec485c0c0bb93d47c69ef7b5dc3/68747470733a2f2f706f7365722e707567782e6f72672f747261646572696e7465726163746976652f66696c7465722d737472696e67732f646f776e6c6f616473)](https://packagist.org/packages/traderinteractive/filter-strings)[![Daily Downloads](https://camo.githubusercontent.com/beaeeac3fc7d0a2dc655743797bdb629730ad4983359fdbd8bbdc1f17b39e9ad/68747470733a2f2f706f7365722e707567782e6f72672f747261646572696e7465726163746976652f66696c7465722d737472696e67732f642f6461696c79)](https://packagist.org/packages/traderinteractive/filter-strings)[![Monthly Downloads](https://camo.githubusercontent.com/5f2389612d675dfbd274765780e6ddbdd0a36d92289c7a2fb2e332f54216b5d0/68747470733a2f2f706f7365722e707567782e6f72672f747261646572696e7465726163746976652f66696c7465722d737472696e67732f642f6d6f6e74686c79)](https://packagist.org/packages/traderinteractive/filter-strings)

A filtering implementation for verifying the contents of strings and some common formats of strings.

Requirements
------------

[](#requirements)

Requires PHP 7.0 or newer and uses composer to install further PHP dependencies. See the [composer specification](composer.json) for more details.

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

[](#installation)

filter-strings-php can be installed for use in your project using [composer](http://getcomposer.org). The recommended way of using this library in your project is to add a `composer.json` file to your project. The following contents would add filter-strings-php as a dependency:

```
composer require traderinteractive/filter-strings
```

### Functionality

[](#functionality)

#### Strings::filter

[](#stringsfilter)

This filter verifies that the argument is a string. The second parameter can be set to `true` to allow null values through without an error (they will stay null and not get converted to false). The last parameters specify the length bounds of the string. The default bounds are 1+, so an empty string fails by default.

The following checks that `$value` is a non-empty string.

```
\TraderInteractive\Filter\Strings::filter($value);
```

#### Strings::concat

[](#stringsconcat)

This filter concatenates the given $value, $prefix and $suffix and returns the resulting string.

```
$value = \TraderInteractive\Filter\Strings::concat('middle', 'begining_', '_end');
assert($value === 'begining_middle_end');
```

#### Strings::translate

[](#stringstranslate)

This filter will accept a string value and return its translated value found in the given $valueMap.

```
$value = \TraderInteractive\Filter\Strings::translate('active', ['inactive' => 'X', 'active' => 'A']);
assert($value === 'A');
```

#### Strings::explode

[](#stringsexplode)

This filter is essentially a wrapper around the built-in [`explode`](http://www.php.net/explode) method with the value first in order to work with the `Filterer`. It also defaults to using `,` as a delimiter. For example:

```
$value = \TraderInteractive\Filter\Strings::explode('abc,def,ghi');
assert($value === ['abc', 'def', 'ghi']);
```

#### Strings::compress

[](#stringscompress)

This filter trims and remove superfluous whitespace from a given string.

```
$value = \TraderInteractive\Filter\Strings::compress(' a string    with lots of    whitespace   ');
assert($value === 'a string with lots of whitespace');
```

This filter can also replace vertical whitespace such as newlines with single spaces.

```
$value = \TraderInteractive\Filter\Strings::compress(" a string\nwith lots\nof    \nnewlines\n   ", true);
assert($value === 'a string with lots of newlines');
```

#### Strings::redact

[](#stringsredact)

This filter will remove specified words from a string or, optionally, replace each letter of the words with a replacement character.

The second argument specifies the words that should be replaced and can either be an array of strings or a callable that returns an array of strings.

The third argument specifies the replacement character. If empty, the words will be removed entirely. If a string with more than one character is provided, only the first character will be used.

```
$value = \TraderInteractive\Filter\Strings::redact('a string with some unwanted words', ['unwanted', 'words'], '*');
assert($value === 'a string with some ******** *****');
```

#### Strings::stripEmoji

[](#stringsstripemoji)

This filter will strip emoji, pictographs, alphanumeric supplement characters and more from a given string.

The second, optional argument specifies a replacement string for the removed characters.

```
\TraderInteractive\Filter\Strings::stripEmoji('🙄 this is ridiculous', ' ');
assert($value === '  this is ridiculous');
```

#### Strings::stripTags

[](#stringsstriptags)

This filter will strip HTML, XML, and PHP tags from a string. This filter also accepts null values, which will be returned as null.

The second, optional argument specifies a replacement string for the removed HTML and XML tags. PHP tags will be stripped without a replacement.

```
\TraderInteractive\Filter\Strings::stripTags('a string withtags', ' ');
assert($value === ' a string with tags ');
```

#### Url::filter

[](#urlfilter)

This filter verifies that the argument is a URL string according to [RFC2396](http://www.faqs.org/rfcs/rfc2396). The second parameter can be set to `true` to allow null values through without an error (they will stay null and not get converted to false).

The following checks that `$value` is a URL.

```
\TraderInteractive\Filter\Url::filter($value);
```

#### Email::filter

[](#emailfilter)

This filter verifies that the argument is an email.

The following checks that `$value` is an email.

```
\TraderInteractive\Filter\Email::filter($value);
```

#### Json::validate

[](#jsonvalidate)

This filter verifies that the value is in a valid JSON format.

The second parameter can be set to `true` to allow null values through without an error.

The third parameter determines the maximum recursion depth that is allowed.

The following checks that `$value` is a valid JSON string.

```
\TraderInteractive\Filter\Json::validate($value);
```

#### Json::parse

[](#jsonparse)

This filter parses a valid JSON string into an array, int, double, or bool. Invalid JSON will throw an error.

The second parameter can be set to `true` to allow null values through without an error.

The third parameter determines the maximum recursion depth that is allowed.

The following checks that `$value` is a valid JSON string and parses it into an array.

```
$value = '{ "string": "value", "array": [1, 2, 3] }';
\TraderInteractive\Filter\Json::parse($value);
assert($value === ['string' => 'value', 'array' => [1, 2, 3]]);
```

#### UuidFilter::filter

[](#uuidfilterfilter)

This filter verifies a given string is a valid universally unique identifier.

The second parameter can be set to `true` to allow null values through without an error.

The third parameter can be set to `true` to allow [Nil UUIDs](https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.7) values through without an error.

The fourth parameter determines which UUID version the value will be validated against. By default, the filter will succeed if the values matches version 1, 2, 3, 4, 5, 6, or 7

```
// Filtering an UUID string
$value = '1a42403c-a29d-11ef-b864-0242ac120002';
$filtered = \TraderInteractive\Filter\UuidFilter::filter($value);
assert($value === $filtered);

// Filtering null values
$value = null;
$filtered = \TraderInteractive\Filter\UuidFilter::filter($value, true);
assert(null === $filtered);

// Filtering a nil UUID
$value = '00000000-0000-0000-0000-000000000000';
$filtered = \TraderInteractive\Filter\UuidFilter::filter($value, false, true);
assert($value === $filtered);

// Filtering for only UUID v4
$value = '1a42403c-a29d-41ef-b864-0242ac120002';
$filtered = \TraderInteractive\Filter\UuidFilter::filter($value, false, false, [4]);
assert($value === $filtered);
```

#### XmlFilter::filter

[](#xmlfilterfilter)

This filter ensures the given string is valid XML.

```
$value = "value";
$filtered = \TraderInteractive\Filter\XmlFilter::filter($value);
assert($value === $filtered);
```

#### XmlFilter::extract

[](#xmlfilterextract)

This filter accepts an XML string and an xpath. It will return the single element found at the xpath.

```
$value =
