HTML comments scraper in PHP

I look around, but have not yet found a solution. I am trying to clear an HTML document and get the text between two comments, but so far I have not been able to do this successfully.

I am using PHP and have tried the PHP Simple DOM parser, recommended here many times, but can't seem to get it to do what I want.

Here (part) of the page I want to analyze:

<div class="class">
  <!-- blah -->
    text
  <!-- end blah -->

  Text I want

  <!-- blah -->
    text
  <!-- end blah -->
</div>

thanks

+3
source share
2 answers

, (.. "" ), strpos, . .

$startStr = '<!-- end blah1 -->';
$endStr = '<!-- start blah2 -->';

$startPos = strpos($HTML, $startStr) + strlen($startStr);
$endPos = strpos($HTML, $endStr );

$textYouWant = substr($HTML, $startPos, $endPos-$startPos);

, , "", strpos offset

+4

, ?

$text = '
<div class="class">
  <!-- blah -->
    text
  <!-- end blah -->

  Text I want

  <!-- blah -->
    text
  <!-- end blah -->
</div>
';

$regex = '/(<!-- end blah -->)(.*?)(<!-- blah -->)/ims';
$match = preg_match_all ($regex, $text, $matches);
+4

Source: https://habr.com/ru/post/1716070/


All Articles