【问题标题】:How to detect tag content in tags and replace(move) them in other tags content place?如何检测标签中的标签内容并将它们替换(移动)到其他标签内容位置?
【发布时间】:2017-09-19 08:23:04
【问题描述】:

我是 php 的初学者,特别是正则表达式的东西,所以我想做的是从像这样的其他标签中的标签中获取内容 <td> <br>(content)<br> </td> 到目前为止我所做的是将数据放入数组块中

include_once('simple_html_dom.php');

$url = '(url_here)';

$htmlstr = str_get_html(file_get_contents($url));
$matches1 = [];
preg_match_all("'<td>(.*?)(</td>|</<td>)'si", $htmlstr, $matches1);
$data1 = array_chunk($matches1[0], 6, FALSE);

但问题是在一些带有 td 标签的行中,我得到的 br 标签内容也像这样

如何检测那些内部标签,如何获取该内容并将其移动到其他其他标签内容空间?

这是我在某些行中遇到的示例link_here

【问题讨论】:

  • 您应该添加一个更好的示例,因为&lt;br&gt; 是一个没有子文本节点的自闭合标签,并且不包含(content)
  • 不要在 cmets 中放置指向 png 的链接,编辑您的问题以编写一个很好且解释清楚的示例。请阅读:stackoverflow.com/help/how-to-ask

标签: php regex


【解决方案1】:

不要对 HTML 使用正则表达式,since there is no and will never be a 100% working solution

改用 HTML 解析器,如内置解析器 (DOMDocument) 或用户贡献的解析器 (如 PHP-HTML-Parser)

当使用 DOMDocument 时,你可以选择你想要的标签,有点像 javascript 可以:

$dom = new DOMDocument();
$dom->loadHTML($yourHTML);

$tds = $dom->getElementsByTagName('td');

【讨论】:

  • @Maluel,你说得对,谢谢。对于那些有熟悉问题的人,找到一个有用的链接link_here
猜你喜欢
  • 2022-12-08
  • 2015-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多