Owe*_*wen 12
$string = '<!-- LoginStart --><div id="stuff">text</div><!-- LoginEnds -->';
$regex = '#<!--\s*LoginStart\s*-->(.*?)<!--\s*LoginEnds\s*-->#s';
preg_match($regex, $string, $matches);
print_r($matches); // $matches[1] = <div id="stuff">text</div>
Run Code Online (Sandbox Code Playgroud)
解释:
(.*?) = non greedy match (match the first <!-- LoginEnds --> it finds
s = modifier in $regex (end of the variable) allows multiline matches
such as '<!-- LoginStart -->stuff
more stuff
<!-- LoginEnds -->'
Run Code Online (Sandbox Code Playgroud)