Created
July 12, 2019 00:24
-
-
Save Juddling/93fbd0fe8329279f2df40981fa3527d3 to your computer and use it in GitHub Desktop.
Regex for removing a BB-style shortcode from content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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