Created
July 26, 2012 15:50
-
-
Save anonymous/3182845 to your computer and use it in GitHub Desktop.
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 | |
| $content =' | |
| <h1 id="my_id" style="font-weight:bold" class="my_class">This is h1</h1> | |
| <div class="my_desc">This is example</div> | |
| <h1 class="h2_class">This is h1 no class</h1> | |
| <h2 style="color:red;">This is h2</h2> | |
| '; | |
| function replace_content($content) { | |
| $content = preg_replace( '/<h(\d{1,6})(.*?)>(.*?)<\/h(\d{1,6}).*?>/', '<h$1 $2><span>$3</span></h$4>', $content ); | |
| $content = custom_strip_attr($content, array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'), array('style')); | |
| return $content; | |
| } | |
| function custom_strip_attr( $text, $elements, $attributes, $two_passes = true ) { | |
| $elements_pattern = implode( '|', (array) $elements ); | |
| /** Build patterns */ | |
| $patterns = array(); | |
| foreach ( (array) $attributes as $attribute ) { | |
| /** Opening tags */ | |
| $patterns[] = sprintf( '~(<(?:%s)[^>]*)\s+%s=[\\\'"][^\\\'"]+[\\\'"]([^>]*[^>]*>)~', $elements_pattern, $attribute ); | |
| /** Self closing tags */ | |
| $patterns[] = sprintf( '~(<(?:%s)[^>]*)\s+%s=[\\\'"][^\\\'"]+[\\\'"]([^>]*[^/]+/>)~', $elements_pattern, $attribute ); | |
| } | |
| /** First pass */ | |
| $text = preg_replace( $patterns, '$1$2', $text ); | |
| if ( $two_passes ) /** Second pass */ | |
| $text = preg_replace( $patterns, '$1$2', $text ); | |
| return $text; | |
| } | |
| echo replace_content($content); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment