PHP的预习比赛 - 如何获得HTML标记? [英] php preg match - how to get html tag?

查看:91
本文介绍了PHP的预习比赛 - 如何获得HTML标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于它的线索,但我仍然需要帮助。
我需要使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆