P
P
phpavel2019-03-26 15:59:10
PHP
phpavel, 2019-03-26 15:59:10

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

How to cut out all the code that lies between the header and footer tags?

$str='
<header></header>
<p>content<p>
<footer></footer>'

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
DanKud, 2019-03-26
@phpavel

Why bother with substrin this case and look for the position of the occurrence of characters in the string? You can pull it out regularly without problems:

$str='
<header></header>
<p>content<p>
<footer></footer>';

preg_match('#</header>(.*?)<footer>#s', $str, $m);
$content = $m[1];
echo $content; /* <p>content<p> */

S
SagePtr, 2019-03-26
@SagePtr

Find the coordinates of these occurrences, substitute ... But it's best to master regular expressions, they are much easier.

M
Marat Garifullin, 2019-03-26
@magarif

Something like this

$start = stripos($str, '</header>' ) + strlen('</header>');
$end  = strripos($str, '<footer>');

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question