Replenish div tags by p in php keeping classes and / or styles [duplicated]

1

Hello friends, I have this doubt

as <div> tags by <p> in php but ...

I have <div> tags that have classes and / or styles that I want to keep with their respective% </div>

Try this but delete the </div> that I want to keep

str_replace('<div>', '<p>', $body);
str_replace('</div>', '</p>', $body);

this is my code

<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>

I would like it to be like this:

<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>

I just want to keep the divs that have class or style with its% </div>

Could you help me please

    
asked by skycomputer2 23.02.2017 в 19:38
source

2 answers

2

You can use the preg_replace function which supports regular expressions:

$html = "<div class='caso-1'>Texto dentro del div</div>";

$html = preg_replace('/div/', 'p', $html);
var_dump(htmlentities($html));
//resulta: string(52) "<p class='caso-1'>Texto dentro del p</p>"

$html1 = "<div class='caso-2' style='align-content: center'>Texto dentro del div</div>";

$div = array('/\<div(.*)>/', '/\<\/div>/');
$html1 = preg_replace($div, array('<p$1>', '</p>'), $html1);
var_dump(htmlentities($html1));
//resulta: string(84) "<p class='caso-2' style='align-content: center'>Texto dentro del div</p>"

As you can see, the first option replaces all the cases in which you find the text div the second option is already more polished, but you can search for a different regular expression

    
answered by 23.02.2017 в 19:56
0

You have several alternatives.

The best option would be to use an HTML parser.

That said, if you want to continue using regular expressions, you have these two options:

Option 1:

Use the following regular expression <div>([^<]*?)<\/div> and replace with <p>$1</p>

This is the simplest option, but it will only work in the case of not having nested html tags and also with the condition that inside the text you do not find the character '<'.

That is, these two cases would not work:

<div> <span>hola</span> </div>
<div> 3<4 </div>

But it would work with the example you have set. Demo .

Option 2:

This method will be used to avoid the previous limitation. Through a loop and recursive regular expression, we will make substitutions.

The regular expression is the following <div>((?:<div[^>]*>(?1)<\/div>|[^<]*|<(?!div))*)<\/div> (flags: m, s, i) and replaced by <p>$1</p> Demo

However, this will replace a single level if you find nested labels. For example, if your entry is:

<div><div>hola</div></div>

It will change it to

<p><div>hola</div></p>

With what you should run it once more to get:

<p><p>hola</p></p>

Basically you repeat the substitution until the previous string is equal to the new one (means there is nothing additional to replace)

Here is an example mounted with php:

<?php

$prevhtml = '
<div class="foo">
<div>FOO</div>

  <div class="bar">
     <div>BAR</div>
  </div>

  <div>FOOBAR<div class="foobar"></div></div>

  <div class="bar"><div><b>BAR</b></div></div>

  <!-- para este caso, deberás ejecutar preg_replace 
       hasta que no haya nada nuevo por reemplazar -->
  <div><div>foobar</div></div>
</div>

';

$newhtml = '';
while($newhtml != $prevhtml) {
    if (!empty($newhtml)) $prevhtml = $newhtml;
    $newhtml = preg_replace(
         '/<div>((?:<div[^>]*>(?1)<\/div>|[^<]*|<(?!div))*)<\/div>/smi'
        ,'<p>$1</p>'
        ,$prevhtml);
}

print $newhtml;

You also have the code here so you can see it running.

    
answered by 07.08.2018 в 12:07