3

I've been looking around but have yet to find a solution. I'm trying to scrape an HTML document and get the text between two comments however have been unable to do this successfully so far.

I'm 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's (part of) the page that I wish to parse:

<div>  <!-- blah -->    text  <!-- end blah -->  Text I want  <!-- blah -->    text  <!-- end blah --></div>

Thanks

Charles Stewart's user avatar
Charles Stewart
11.8k4 gold badges49 silver badges86 bronze badges
askedAug 26, 2009 at 5:55
Pep's user avatar
1
  • Could you show us your current code?CommentedAug 26, 2009 at 6:01

2 Answers2

4

Assuming that each comment is different (i.e. "blah" is not the same in the first and second sections), you can use some simplestrpos to grab everything between them.Regular expressions are not necessary.

$startStr = '<!-- end blah1 -->';$endStr = '<!-- start blah2 -->';$startPos = strpos($HTML, $startStr) + strlen($startStr);$endPos = strpos($HTML, $endStr );$textYouWant = substr($HTML, $startPos, $endPos-$startPos);

If the two sets of commentsare the same, you'll need to modify this to find the second "blah", usingstrpos'soffset parameter

answeredAug 26, 2009 at 12:00
DisgruntledGoat's user avatar
Sign up to request clarification or add additional context in comments.

Comments

4

Maybe you can use regular expressions?

$text = '<div>  <!-- blah -->    text  <!-- end blah -->  Text I want  <!-- blah -->    text  <!-- end blah --></div>';$regex = '/(<!-- end blah -->)(.*?)(<!-- blah -->)/ims';$match = preg_match_all ($regex, $text, $matches);
answeredAug 26, 2009 at 6:14
Deniss Kozlovs's user avatar

2 Comments

Obligatory "now you have two problems" comment ;)
"Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins".

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.