php dom replace html tags

0

Hello friends, I would like to know how to replace some html tags, replace div tags that do not have class or style per tag p

I have this code:

<div>
    Lorem ipsum dolor sit amet
    <div>
        Lorem ipsum dolor sit amet
    </div>
    <div>
        Lorem ipsum dolor sit amet
    </div>
    <div>
        <div class="media_embed">
                <div style="xxx">
                    <div style="xxx">
                        <div style="xxx"></div>
                    </div>
                </div>
        </div>
    </div>
    <div>
        Lorem ipsum dolor sit amet
    </div>
</div>

and I would like it to be this way:

<p>
    Lorem ipsum dolor sit amet
    <p>
        Lorem ipsum dolor sit amet
    </p>
    <p>
        Lorem ipsum dolor sit amet
    </p>
    <p>
        <div class="media_embed">
                <div style="xxx">
                    <div style="xxx">
                        <div style="xxx"></div>
                    </div>
                </div>
        </div>
    </p>
    <p>
        Lorem ipsum dolor sit amet
    </p>
</p>

try this way but I did not get it: here is my php code

<?php
      $embeds= $dom->getElementsByTagName('div');
      foreach ($embeds as $embed) {
          $class = $embed->getAttribute('class');
          if ($class == "")
          {
            $link= $dom->createElement('p');
            $embed->parentNode->replaceChild($link, $embed);
          }
      }
?>

Could you please help me?

    
asked by skycomputer2 24.02.2017 в 02:29
source

2 answers

2

Using PHP:DOM , you could do it like this:

<?php

$html = '<div>
    Lorem ipsum dolor sit amet
    <div>
        Lorem ipsum dolor sit amet
    </div>
    <div>
        Lorem ipsum dolor sit amet
    </div>
    <div>
        <div class="media_embed">
                <div style="xxx">
                    <div style="xxx">
                        <div style="background-color:red;"><p>Rojo</p></div>
                    </div>
                </div>
        </div>
    </div>
    <div>
        Lorem ipsum dolor sit amet
    </div>
</div>';

$doc = new DOMDocument();
$doc->loadHTML($html);

$nodeList = $doc->getElementsByTagName('div');

// Convertimos la "nodeList" a arreglo
// de lo contrario al pasar "divs" hijos a un "p"
// se pierde la referencia en la "nodeList"
$divs = iterator_to_array($nodeList);

foreach ($divs as $div) {

    // Si no tiene atributos
    if (!$div->hasAttributes()) {

        $p = $doc->createElement('p');

        // Trasladamos todos los hijos del "div" al nuevo "p"
        while ($div->hasChildNodes()) {
            $p->appendChild($div->firstChild);
        }

        // Reemplazamos el "div" por el "p"
        $div->parentNode->replaceChild($p, $div);
    }
}

echo $doc->saveHTML();

Demo

    
answered by 24.02.2017 в 16:37
0

EDIT:

A. USING PHP

Test php code .

Note: The html was formatted correctly, as there were for example divs without closing in the html of the question

$html='<div>Lorem ipsum dolor sit amet</div>
<div>Lorem ipsum dolor sit amet</div>
<div>Lorem ipsum dolor sit amet</div>
<div class="media_embed"> </div>
<div style="xxx"></div>
<div style="xxx"></div>
<div style="background-color:red;"><p>Rojo</p></div>
<div>Lorem ipsum dolor sit amet</div>';
$regex = '#<div>(.+?)</div>#s';
$result = preg_replace( $regex, 
    '<p>$1</p>', 
    $html
);
echo $result;

Result:

<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<div class="media_embed"> </div>
<div style="xxx"></div>
<div style="xxx"></div>
<div style="background-color:red;"><p>Rojo</p></div>
<p>Lorem ipsum dolor sit amet</p>

B. USING JQUERY:

Try fiddle . I have put a red color in one that is not changed so you can see the result.

Simply with this in jQuery:

$('div:not([class])').contents().unwrap().wrap('<p/>');
    
answered by 24.02.2017 в 03:48