user1219706
user1219706

Reputation: 9

PHP: Extract text from string between two specific tags

I'm putting together a quick extracting function in PHP. Basically, it will read the contents of a given URL and extract the necessary HTML code. It will then put the code into a new page. Now, I've gotten everything working except one thing.

I have a string with the contents of the URL, and now I want to extract text between two tags. Let's say I want to start copying content at a certain tag. That's easy since those tags have classes.

So I'd have the function start with "" It simply searches the document for all text that matches this. I've run into a problem, though. Within the section I wish to copy, there are other tags. As such, if I tell the function to stop extracting at "," the scraper will stop reading at the end of a random tag. Can someone help me write a bit of PHP code that will ensure that the tag the function stops at is the closing tag for the "" and not for some random tag that happens to be in the string?

Thanks in advance.

Upvotes: 0

Views: 884

Answers (1)

scibuff
scibuff

Reputation: 13755

There is no way to do this and account for every possible content between the tags. Use XmlDocument and parse the HTML.

Upvotes: 3

Related Questions