PHP DOM 搜索 HTML 并指定 P 中 IMG 的位置
我正在寻找解析一些从 ckeditor 提交的 HTML。发布的 HTML 如下所示:(
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">#012<html><body><p>Text Before <img alt="HAMBURGER" height="20" src="/sites/all/modules/ckeditor/plugins/apoji/images/emoji-E120.png" title="HAMBURGER" width="20"> Text After</p></body></html>
格式化,不声明一致性):
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<body>
<p>
Text Before
<img alt="HAMBURGER" height="20" src="/sites/all/modules/ckeditor/plugins/apoji/images/emoji-E120.png" title="HAMBURGER" width="20">
Text After
</p>
</body>
</html>
我一直在寻找使用如下所示的内容:
$DOM = new DOMDocument;
$DOM->loadHTML($input);
$items = $DOM->getElementsByTagName('*');
foreach ($items as $item) {
switch ($item->nodeName) {
case "p":
$sms .= $item->nodeValue."\n";
break;
case "img":
$img_out .= "IMG Attr: ".$item->getAttribute('title')."\n";
break;
}
}
我的目标是创建一个纯文本字符串,根据其标题替换图像,所以我会有一个像这样的字符串:
Text Before HAMBURGER Text After
我已经开始沿着 DOM 路线走下去,因为这似乎是最好的方法,但现在我有两个问题:
- 如果我像上面那样循环遍历文档,IMG 将在文本之后结束, 不在其中。我怎样才能避免这种情况呢?
- 从 DOM 文档中提取所有纯文本的最佳方法,保持项目的顺序(链接到第 1 点)。
预先感谢任何能给我一些意见的人。
I'm looking to parse some HTML which is submitted from ckeditor. The HTML which is posted looks like the below:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">#012<html><body><p>Text Before <img alt="HAMBURGER" height="20" src="/sites/all/modules/ckeditor/plugins/apoji/images/emoji-E120.png" title="HAMBURGER" width="20"> Text After</p></body></html>
(formatted, without claiming congruency):
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<body>
<p>
Text Before
<img alt="HAMBURGER" height="20" src="/sites/all/modules/ckeditor/plugins/apoji/images/emoji-E120.png" title="HAMBURGER" width="20">
Text After
</p>
</body>
</html>
I've been looking to use something like the below:
$DOM = new DOMDocument;
$DOM->loadHTML($input);
$items = $DOM->getElementsByTagName('*');
foreach ($items as $item) {
switch ($item->nodeName) {
case "p":
$sms .= $item->nodeValue."\n";
break;
case "img":
$img_out .= "IMG Attr: ".$item->getAttribute('title')."\n";
break;
}
}
My aim to to create a plain text string, replacing the image based on its title, so I'd have a string like:
Text Before HAMBURGER Text After
I've started going down the DOM route, as it seems the best way to do it, but now I have two questions:
- If I loop over the document as above the IMG ends up AFTER the text,
not in the middle of it. How could I avoid this? - The best way to extract all the plain text from the DOM document, keeping the order of items (linked to point 1).
Thanks in advance to anyone that can give me some input in to this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个选项是使用 XPath 查询来选择所需的文本/标题,并输出它们各自的值。
An option is to use an XPath query to select the text/titles that you want, and output their respective values.
您可以使用 XPath 查找特定项目,然后使用 用新节点替换它们。
例如
打印
对于您的问题#2:请详细说明。可以像
echo $doc->documentElement->textContent
一样简单。但也可能最终使用 XSL(T)You can use XPath to find specific items and then replace them with new nodes.
E.g.
prints
For your question #2: please elaborate. Can be as simple as
echo $doc->documentElement->textContent
. But could also end up using XSL(T)您可以简单地使用正则表达式替换:
You could simply use a regular expression replacement: