【问题标题】:in PHP How to regex preg_match specific HTML tag value from same type value tags在 PHP 如何正则表达式 preg_match 来自相同类型值标签的特定 HTML 标签值
【发布时间】:2016-02-21 01:22:37
【问题描述】:

在 PHP 如何正则表达式 preg_match 来自相同类型和值标签的特定标签值。

这里有一些 html 标签,我想通过正则表达式 preg_match 获取最后一个标签值

</tr>
<tr class="flow">
<td>14.1%</td>
<td>13.2%</td>
<td>13.6%</td>
<td>14.1%</td>
<td>14.0%</td>
<td>14.0%</td>
<td>14.1%</td> <== i want to fetch this last <td> tag value with regex, how ?
</tr>
</tbody>

If I preg_match `'/<tr class="flow">(.*?)<\/td>/s'`; 
but then it will fetch first `<td>` tag value 
and if preg_match `'/<td>(.*?)<\/td>/s'`; 
then it will fetch all `<td>` tags values. 

So how I can fetch last `<td>` value?

I mean I want something like to `preg_match` in reverse mode 
like this `'/</tbody>(.*?)<\td>/s'`; or maybe anything else to do?


This is the small part of page and page contain 100's of other <td> tags
so you must have to do something preg_match with 
between </tbody> and <td> in reverse mode ...OR.. 
Between <tr class="flow">  </tbody> in forward mode. 

但我不知道怎么做?您的帮助将不胜感激。

【问题讨论】:

  • 不,你的回答并不能解决我的问题,这很复杂,难以理解。所以请在这里给我一个例子,我如何 preg_match 最后一个 标记值。我必须使用什么公式?

标签: php html regex preg-match


【解决方案1】:
preg_match('|\<tr class="flow"\>(.*?)\</tr\>|s', $input_text, $match);
$filterd_input_text = $match[1];
preg_match_all('|\<td\>(.*?)\</td\>|', $filterd_input_text, $matches);
$value = end($matches[1]);
var_dump($value);

preg_match_all('|<td>(.*?)</td>.*</tbody>|s', $t, $matches);

【讨论】:

  • 对不起,它无济于事,因为这是 html 页面的一小部分,并且 html 页面包含 100 个其他 标签,所以你必须在 和
  • @smallbee 你能把完整的 html 的 url 贴出来吗?还是粘贴到pastebin
  • 你可以使用两个 preg_match_all。首先找到相关的html。第二个找到你的价值
  • 您能否再次编辑您的答案以在此处给我一个示例,我们将不胜感激。
猜你喜欢
相关资源
最近更新 更多
热门标签