How to use substr() to cut out a piece of code from tag to tag?

How to cut all the code that lies between the tags header and footer?
March 19th 20 at 08:40
3 answers
March 19th 20 at 08:42
Why the trouble with substr in this case, and to seek positions of occurrence of characters in a string? Can the regular season to pull without problems:

preg_match('#</header>(.*?)<footer>#s', $str, $m);
$content = $m[1];
echo $content; /* <p>content<p> */
thank you, and how to make a header that would be inclusive? - Gracie_Grant18 commented on March 19th 20 at 08:45
@Gracie_Grant18, specifically in these lines that you have specified, then:

preg_match('#^(.*?)<footer>#s', $str, $m);

But I understand that this is just an excerpt from the full HTML code. This is in full to get the<header> you just need to insert the regular expression for a tag that precedes it:

preg_match('#<тэг_перед_header>(.*?)<footer>#s', $str, $m);
- carmella.Kautz commented on March 19th 20 at 08:48
Then ajtishnikam how? If you want for example to id=footer? - Gracie_Grant18 commented on March 19th 20 at 08:51
@Gracie_Grant18with such queries you have to look towards third-party libraries for parsing. To parse it with regexps, of course, but will be uncomfortable:
dom-crawler + css selector - carmella.Kautz commented on March 19th 20 at 08:54
March 19th 20 at 08:44
Find the coordinates of these occurrences, to substitute... But the best master of the regular season, they are much more easier.
March 19th 20 at 08:46
$start = stripos($str, '</header>' ) + strlen('</header>');
$end = strripos($str, '<footer>');

$result = substr($str, $start, $end);

Find more questions by tags ParsingHTMLPHP