Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove tags from the beginning and end of the string with PHP

Tags:

string

php

tags

I need to get a string without the tags at the beginning and end of the string. I know strip_tags but as you know it removes all tags. For example:

<span class="note">
  <span></span>This is my text <strong>Hello world</strong> 
</span>

In this example i want to keep the <strong> tags. Important to know that i want a unconditional solution because the tags can be different. I know already that you can exclude tags with strip_tags (that is not what i want to know) but that is not the case.

Does anyone know a fast solution to this, for example a reg expression replace or something (the thing i am not good at). Cannot find such thing on the net so that's why i ask it here.

Thank you for the contribution.

like image 335
Codebeat Avatar asked Sep 02 '25 02:09

Codebeat


1 Answers

DOMDocument would be handy for this...

$dom = new DOMDocument;

$dom->loadHTML($html);

$html = '';

foreach($dom->getElementsByTagName('body')->item(0)->firstChild->childNodes as $node) {
    $html .= trim($dom->saveHTML($node));
}

echo $html;

CodePad.

If you were unswayable about using a regex, you could use...

$html = preg_replace('/^\s*<[^>]+>\s*|\s*<\/[^>]+>\s*\z/', '', $html);

CodePad.

Note that a HTML tag with an attribute containing a > would fail the regex. That's why I'd prefer the DOMDocument solution.

Both these solutions will strip leading and trailing whitespace text nodes. If that is not desirable, drop the trim() from the first example and the \s* from the second example.

like image 134
alex Avatar answered Sep 05 '25 02:09

alex