mimrahe / striptags
PHP strip_tags extended lib
Requires (Dev)
- phpunit/phpunit: 5.5.5
This package is not auto-updated.
Last update: 2025-05-10 23:44:05 UTC
README
PHP strip_tags() function only allow to strip tags that are not in a string as second parameter! and I found i need a solution to strip only some tags!
the StripTags is a solution for me now!
How to install
composer require mimrahe/striptags:1.3.0
or download release zip package
How to use
Namespace
\Mimrahe\StripTags\Stripper;
strip only some tags
$stripTags = new Stripper('<b>some bold</b><a href="#">link</a>'); $stripedText = $stripTags->only(['b', 'p'])->strip(); echo $stripedText; // prints 'some bold<a href="#">link</a>'
strip all tags except some tags
echo (new Stripper('<b>some bold</b><a href="#">link</a>'))->except(['a'])->strip(); // prints 'some bold<a href="#">link</a>'
in loop usage example
$stripper = new Stripper(); $stripper->only(['a', 'ul', 'li']); $textArray = [ // some texts that will be stripped ]; foreach($textArray as $text) { echo $stripper->on($text)->strip(); }
giving an Array to Stripper
$stripper = new Stripper(); $stripper->only(['a', 'ul', 'li']); $textArray = [ // some texts that will be stripped ]; $strippedArray = $stripper->on($textArray)->strip();
Tip
- you can give a nested array to method 'on'
filter an array and then stripping
$stripped = $stripper->on($textArray) ->filter(function($value, $key){ // some checks return $bool; }) ->strip();
do something on array before stripping
$stripped = $stripper->on($textArray) ->before(function(&$value, $key){ // do something on array values // note that $value must be as a reference // $key may be a reference }) ->strip();
do something on array items after stripping
$stripped = $stripper->on($textArray) ->after(function(&$value, $key){ // do something on array values // note that $value must be as a reference // $key may be a reference }) ->strip();
Methods
Constructor: Stripper constructor
new Stripper(array | string $subject = '');
parameters:
- $subject: text or array of texts that strippers works on
returns: $this
on: specify string or array of strings stripper works on (same as constructor)
$stripper->on(array | string $subject);
parameters:
- $subject: text or array of texts that stripper works on
returns: $this
only: says strip only this tags
$stripper->only(array $tags);
parameters:
- $tags: array of tags that will be stripped
returns: $this
except: says strip all tags except some (same as strip_tags)
$stripper->except(array $tags);
parameters:
- $tags: array of tags that will not be stripped
returns: $this
Tip
- in a moment only use only() or except() not both
filter: specify a callback which filters subject array
$filter = function($value, $key){ // some check on value return $bool; } $stripper->filter(callable $filter);
parameters:
- $filter: a closure does filter on array
returns: $this
before: specify a callback effects before stripping
$before = function(&$value, $key){ $value = '<br>' . $value; } $stripper->before(callable $before);
parameters:
- $before: a closure effects on array items
returns: $this
after: specify a callback effects on array items after stripping
$after = function(&$value, $key){ $value = trim($value); } $stripper->after(callable $after);
parameters:
- $after: a closure effects on stripped array
returns: $this
strip: strips string or array of strings
$stripper->strip();
returns: stripped string or array of stripped string
License
mimrahe/StripTags is licensed under the MIT License
A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.