Skip to content

Instantly share code, notes, and snippets.

@idokd
Last active September 10, 2021 08:02
Show Gist options
  • Select an option

  • Save idokd/f98baa5815204adbb9052828454d536b to your computer and use it in GitHub Desktop.

Select an option

Save idokd/f98baa5815204adbb9052828454d536b to your computer and use it in GitHub Desktop.
Strip Specific HTML/XML tag attributes
<?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() );
}
@idokd

idokd commented Sep 10, 2021

Copy link
Copy Markdown
Author

So:

$empty = strip_tag_attributes( '', 'input', 'value' );

will provide the empty value tag (no value attribute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment