Last active
September 10, 2021 08:02
-
-
Save idokd/f98baa5815204adbb9052828454d536b to your computer and use it in GitHub Desktop.
Strip Specific HTML/XML tag attributes
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 | |
| // Based and altered from https://www.py4u.net/discuss/24287 | |
| function strip_tag_attributes( $html, $tag, $attr ) { | |
| $domd = new DOMDocument(); | |
| libxml_use_internal_errors( true ); | |
| $domd->loadXML( $html ); | |
| libxml_use_internal_errors( false) ; | |
| $domx = new DOMXPath( $domd ); | |
| $items = $domx->query('//' . $tag . '[@' . $attr . ']'); | |
| foreach( $items as $item ) { | |
| $item->removeAttribute( $attr ); | |
| } | |
| return( $domd->saveXML() ); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So:
$empty = strip_tag_attributes( '', 'input', 'value' );
will provide the empty value tag (no value attribute