Skip to content

Instantly share code, notes, and snippets.

@Juddling
Created July 12, 2019 00:24
Show Gist options
  • Select an option

  • Save Juddling/93fbd0fe8329279f2df40981fa3527d3 to your computer and use it in GitHub Desktop.

Select an option

Save Juddling/93fbd0fe8329279f2df40981fa3527d3 to your computer and use it in GitHub Desktop.
Regex for removing a BB-style shortcode from content
<?php
class StripTag implements Replacement {
private $tagName;
public function __construct( string $tagName ) {
$this->tagName = $tagName;
}
public function replace( string $html ) : string {
$pattern = '/' // modifiers
. "\[{$this->tagName}" // opening tag
. '(?:[^\]]*)' // non-capturing group, match all except ]
. '\]' // opening tag
. '(.*?)' // capture content, non greedy, to match the closest correct closing tag
. "\[\/{$this->tagName}" // closing tag
. '(?:[^\]]*)' // non-capturing group, match all except ]
. '\]' // closing tag
. '/is'; // modifiers, s = allow dot to match new line
return preg_replace( $pattern, '$1', $html );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment