php中如何从字符串中获取html标签?
我有一个从 RSS 提要中提取的 html 输出,它是这样的:
<div>
<p>
Some text
</p>
<iframe src="http://www.source.com"></iframe>
</div>
问题是我只需要 iframe 标记的 attr“src”,有没有办法用 PHP 获取它?也许是正则表达式?
提前致谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您始终只获取上面列出的数据,则可以使用简单的子字符串,使用
src="
和">
编辑 - 修复代码并分成多行。这很容易成为一行,但是 - 我认为如果我分成多行会更容易理解。
If you're consistently getting just the data you listed above, you could use a simple substring, using the string positions of
src="
and"><iframe
to specify which substring you want:EDIT - fixed the code and split into multiple lines. This could easily be a one-liner, but - thought it was easier to understand if I broke into multiple lines.
我推荐 DOMDocument 或 SimpleXML。
像这样的事情可能会给你带来想法。
I'd recommend DOMDocument or SimpleXML.
Something like this might give you the idea.
我不是正则表达式方面的专家,但另一种方法是在
"
标记上使用explode
并获取array[1]
,例如this:这要求您的 RSS feed 非常一致,如果“Some text”部分包含
"
标记,这会造成混乱,您会得到错误的字符串。您可以查看数组中以
http
或www
开头的所有内容来解决错误,但同样,它需要非常一致的 RSS 提要,因此您必须自行判断如果这足以完成工作的话。I'm not an expert with regex, but a alternative way would be to use
explode
on the"
marks and getarray[1]
like this:This requires your RSS feed to be very consistent though, if the "Some text" part were to contain
"
marks, this would mess up and you'd get a wrong string.You could look through the array for everything starting with
http
orwww
to work around errors, but again, it requires a very consistent RSS feed, so you have to judge for you self if this would do the job good enough.您可以使用一些命令行 perl 脚本来解析此输出。这可能非常强大,具体取决于您制作正则表达式的通用程度。
例如,
$command = "echo your_html_output | perl -pe 's/src=\"(.*)\"/$1/'"; # 捕获 src=" 和 "(结束引号)之间的内容
$output = shell_exec("$command");
You could parse this output with a little command line perl script. This can be quite robust depending on how general you make the regular expression.
For example,
$command = "echo your_html_output | perl -pe 's/src=\"(.*)\"/$1/'"; # Capture what is in between src=" and the " (the closing quote)
$output = shell_exec("$command");