PHP:从两个特定标签之间的字符串中提取文本

发布于 2025-01-07 06:35:44 字数 349 浏览 3 评论 0原文

我正在用 PHP 编写一个快速提取函数。基本上,它将读取给定 URL 的内容并提取必要的 HTML 代码。然后它将把代码放入一个新页面中。现在,除了一件事之外,我已经一切正常。

我有一个包含 URL 内容的字符串,现在我想提取两个标签之间的文本。 假设我想开始复制某个标签处的内容。这很容易,因为这些标签有类。

所以我会让该函数以“”开头,它只是在文档中搜索与此匹配的所有文本。不过,我遇到了问题。在我想要复制的部分中,还有其他标签。因此,如果我告诉函数在“”处停止提取,则抓取器将在随机标签的末尾停止读取。有人可以帮我编写一些 PHP 代码,以确保函数停止处的标记是“”的结束标记,而不是字符串中碰巧出现的某个随机标记吗?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深已缘浅 2025-01-14 06:35:44

没有办法做到这一点并考虑标签之间的所有可能的内容。使用 XmlDocument 并解析 HTML。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文