PHP的预习比赛 - 如何获得HTML标记? [英] php preg match - how to get html tag?
问题描述
我需要使用preg_match来从HTML标记中获取我需要的文本。
HTML是:
< TD>
< center>
< b>
< font face =Arialcolor =red>(我需要这个内容)< / font>
< / b>
< / center>
< / td>
(btw。我解决了我的domdocument问题,但我需要使用preg_match)
请帮忙。
问候。
这是什么?
<?php
$ html ='< td>
< center>
< b>
< font face =Arialcolor =red>(我需要这个内容)< / font>
< / b>
< / center>
< / td>';
$ matches = array();
preg_match_all('/< font。*?>(。*?)< \ / font> /是',$ html,$ matches);
var_dump($ matches [1]);
编辑:您可能想包含'face =Arialcolor =red' - 除非你想匹配每个字体元素的内容。您可能还希望包含< b>
和< center>
元素以进一步缩小搜索范围。 / p>
附注:此HTML看起来非常过时。使用中心和字体元素是非常不好的做法。所以使用表格进行布局。
there were many threads about it, but I still need help. I need to use preg_match to get the text I need from HTML tags.
the HTML is:
<td>
<center>
<b>
<font face="Arial" color="red">(I need this content)</font>
</b>
</center>
</td>
(btw. I solved my problem with domdocument, but I need to use preg_match)
Please, help.
Regards.
This?
<?php
$html = '<td>
<center>
<b>
<font face="Arial" color="red">(I need this content)</font>
</b>
</center>
</td>';
$matches = array();
preg_match_all('/<font.*?>(.*?)<\/font>/is', $html, $matches);
var_dump($matches[1]);
Edit: you may want to include the 'face="Arial" color="red"'-bit in the regexp unless you want to match EVERY font-element's contents. You may also wanna include the <b>
and <center>
elements to narrow the search even further.
On a side note: This HTML looks very dated. Using center and font elements is very, very bad practice. So is using tables for layout.
这篇关于PHP的预习比赛 - 如何获得HTML标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!