-1

I have a string that's made up of the following html:

<p> </p>
<h2>Content</h2>
<p>Content</p>
<h2>Content</h2>
<p>Content</p>

How can I remove the first element completely (even it's content) and leave me with the rest of the string?

It should be noted that the first element could be anything or contain anything.

Rob
  • 6,304
  • 24
  • 83
  • 189
  • What do you mean by first element? Just any HTML tag? – Devon Bessemer Jun 20 '18 at 13:14
  • please also check https://stackoverflow.com/questions/1171597/how-to-remove-an-html-element-using-the-domdocument-class - or just search "removing HTML elements from DOM document", you'll find lots of examples – PolishDeveloper Jun 20 '18 at 13:27

4 Answers4

3

You can use the DOM library:

 $document = DOMDocument::loadHTML( $yourString );
 $childToRemove = $document->getElementsByTagName('p')->item(0);
 $childToRemove->parentNode->removeChild($childToRemove);


 //get the string
 return $document->saveHTML();
PolishDeveloper
  • 930
  • 5
  • 17
  • 1
    OP says the first element can _be_ anything... so maybe `$document->item(0);`? – OK sure Jun 20 '18 at 13:21
  • thats just an example, the http://php.net/manual/en/class.domdocument.php is pretty good and full of examples. The question looks like "how to remove something from DOM structure" – PolishDeveloper Jun 20 '18 at 13:25
0

You can explode on line breaks and remove the first element with array_shift:

$html = '<p> </p>
    <h2>Content</h2>
    <p>Content</p>
    <h2>Content</h2>
    <p>Content</p>';
$lines = explode(PHP_EOL, $html); // break into lines
array_shift($lines); // remove first element
$newHtml = implode(PHP_EOL, $lines); // rejoin lines
echo $newHtml;
OK sure
  • 2,617
  • 16
  • 29
0

You could use something like this:

$split = explode("<h2>", $str);
unset($split[0]);
$str = "<h2>" + implode("<h2>", $split);
PhilMasteG
  • 3,095
  • 1
  • 20
  • 27
0

DOM manipulation may be the best here but the following regex is will replace the tags it finds.

<[^/]*/.*>

Essentially, match < and everything up until it finds a /.*> so it will work for a self closing tag like <br/> or a standard <p> </p> but it won't work for an unclosed HTML tag like <br>.

For preg_replace, you can specify the 4th argument to limit it to only one replacement:

preg_replace("/<[^\/]+\/.*>/", "", $str, 1);
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95