-2

Code:

    <div>
      <font face="Arial, Verdana">
         <span style="font-size: 13.3333px;">
           <u>
             Hello World
           </u>
       </span>
    </font>
 </div>
    <div>
      <font face="Arial, Verdana">
         <span style="font-size: 13.3333px;">
           <u>
            Hello World2
           </u>
            <br>
       </span>
    </font>
 </div>
 <div>
    <br>
 </div>
 <div>
      <font face="Arial, Verdana">
        <span style="font-size: 13.3333px;">
           <u>
               <br>
            </u>
        </span>
      </font>
 </div>

Output:- I wish to get exactly the same output as given below

    <div>
      <font face="Arial, Verdana">
         <span style="font-size: 13.3333px;">
            <u>
             Hello World
            </u>
          </span>
       </font>
     </div>
    <div>
      <font face="Arial, Verdana">
         <span style="font-size: 13.3333px;">
           <u>
            Hello World2
           </u>
            <br>
       </span>
    </font>
 </div>
 <br>
 <br>

Here is what I have tried:

$html = preg_replace("/<div.*?>.*?<br.*?>.*?<\/div>/", "", $html);

but it is not working fine.

Please take a look on the code and suggest me how I can replace "div" tag from its starting to corresponding closing tag with "br" tag only when there is no text is present under "div" tag. As shown above in second "div" tag.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ram goel
  • 17
  • 4

2 Answers2

2

use domDocument to manipulate with html structure

$doc = new DOMDocument();
$doc->loadHtml($pageHtml);
$x = new DOMXpath($doc);
foreach($x->query('//div[normalize-space(.) = ""]') as $div) {
    $link= $doc->createElement('br');
    $div->parentNode->replaceChild($link, $div);
}

echo $doc->saveHTML();

demo

splash58
  • 26,043
  • 3
  • 22
  • 34
2

I would highly suggest using DOM Manipulation to accomplish this. You can use regular expressions and you can make other solutions work. However, DOM Manipulation was created for this exact reason.

There are many examples of DOM Parsers in PHP. Some are slower than others. Check out this SO post for a great listing of potential candidates for DOM Parsers.

You could always use Regular Expressions - but here's the condition under which I would personally use a Regular Expression: If you never plan to add any other functionality to this. If you do plan to add more, change it up, make your script more versatile, etc., then I'd say don't use a RegEx. The reason is you will either end up with a huge, completely daunting expression, or you'll end up with many small "one-off" expressions. It will take you less time to reference a function inside a DOM Parser than it will to figure out the proper Regular Expression.

EDIT:

I've removed my code snippet - Splash58's answer is a more elegant solution. His example uses native PHP which 9.9 times out of 10 is better (quicker, more efficient, more community support, etc).

Community
  • 1
  • 1
Nate I
  • 946
  • 4
  • 10