Skip to content

Instantly share code, notes, and snippets.

@mendesalexandre
Forked from herveguetin/replace_html_tags.php
Created November 14, 2024 01:30
Show Gist options
  • Select an option

  • Save mendesalexandre/cf6f89c6b88eed46ef6dcbd9a9a896b9 to your computer and use it in GitHub Desktop.

Select an option

Save mendesalexandre/cf6f89c6b88eed46ef6dcbd9a9a896b9 to your computer and use it in GitHub Desktop.

Revisions

  1. @herveguetin herveguetin revised this gist Oct 10, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion replace_html_tags.php
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,7 @@

    // Create a Zend_Dom_Query object in order to find tags to replace
    $html = '<dt>Some DT</dt><dd><div>Some DD content</div></dd>'; // Your source HTML
    $html = utf8_decode($html);
    $domQuery = new Zend_Dom_Query($html);

    // Create DOM content for $newItem
    @@ -59,4 +60,4 @@

    // Generate HTML out of complete $newItem
    $updatedHtml = $newItem->saveHTML();
    return $updatedHtml;
    return utf8_encode($updatedHtml);
  2. @herveguetin herveguetin revised this gist Sep 9, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion replace_html_tags.php
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@
    $newDomElement->setAttribute($attribute, $value);
    }

    // Inject all child nodes from $searchedElement into new DOM element
    // Inject all child nodes from $searchedElement into the new DOM element
    $children = $searchedDomElement->childNodes;
    foreach($children as $child) {

  3. @herveguetin herveguetin revised this gist Sep 9, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions replace_html_tags.php
    Original file line number Diff line number Diff line change
    @@ -4,14 +4,16 @@
    'tag' => 'div', // ... <div> tag...
    'attributes' => array( // ... with these attributes
    'class' => 'some-class',
    'id' => 'some_id'
    'id' => 'some_id',
    'data-something' => 'some-value'
    )
    ),
    'dd' => array(
    'tag' => 'div',
    'attributes' => array(
    'class' => 'some-other-class',
    'id' => 'some_other_id'
    'id' => 'some_other_id',
    'data-something' => 'some-other-value'
    )
    )
    );
  4. @herveguetin herveguetin revised this gist Sep 9, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions replace_html_tags.php
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,15 @@
    'dt' => array( // <dt> tag is replaced by...
    'tag' => 'div', // ... <div> tag...
    'attributes' => array( // ... with these attributes
    'class' => 'option-title ' . $option->getTitle() . '-option-title'
    'class' => 'some-class',
    'id' => 'some_id'
    )
    ),
    'dd' => array(
    'tag' => 'div',
    'attributes' => array(
    'class' => 'option-content ' . $option->getTitle() . '-option-content'
    'class' => 'some-other-class',
    'id' => 'some_other_id'
    )
    )
    );
  5. @herveguetin herveguetin created this gist Sep 9, 2014.
    58 changes: 58 additions & 0 deletions replace_html_tags.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    <?php
    $replacedTags = array(
    'dt' => array( // <dt> tag is replaced by...
    'tag' => 'div', // ... <div> tag...
    'attributes' => array( // ... with these attributes
    'class' => 'option-title ' . $option->getTitle() . '-option-title'
    )
    ),
    'dd' => array(
    'tag' => 'div',
    'attributes' => array(
    'class' => 'option-content ' . $option->getTitle() . '-option-content'
    )
    )
    );

    // Create a new item that will contain our new DOM elements
    $newItem = new DOMDocument();

    // Create a Zend_Dom_Query object in order to find tags to replace
    $html = '<dt>Some DT</dt><dd><div>Some DD content</div></dd>'; // Your source HTML
    $domQuery = new Zend_Dom_Query($html);

    // Create DOM content for $newItem
    foreach ($replacedTags as $tag => $config) {

    // Find DOM elements matching the tag to replace
    $searchedDomElements = $domQuery->query($tag);

    foreach ($searchedDomElements as $searchedDomElement) {

    // Create a new DOM element in $newItem with new tag...
    $newDomElement = $newItem->createElement($config['tag']);

    // ... and set updated attributes on this new DOM element
    foreach($config['attributes'] as $attribute => $value) {
    $newDomElement->setAttribute($attribute, $value);
    }

    // Inject all child nodes from $searchedElement into new DOM element
    $children = $searchedDomElement->childNodes;
    foreach($children as $child) {

    // Create a DOM element in $newItem that is a clone of $child and with all $child's nodes...
    $newDomElementChild = $newItem->importNode($child, true);

    // ... and append this DOM element to the new DOM element
    $newDomElement->appendChild($newDomElementChild);
    }

    // Once the new DOM element is ready, append it to the $newItem
    $newItem->appendChild($newDomElement);
    }
    }

    // Generate HTML out of complete $newItem
    $updatedHtml = $newItem->saveHTML();
    return $updatedHtml;