正则表达式找到这种模式

时间:2010-12-08 21:54:26

标签: php regex

PHP =&gt;如何以这样一种方式搜索这个字符串:当我class="font8text">N</span>'给我'EARLL'时,它在下一个<span>

<div align="left" style=";">    
<span style="width:15px; padding:1px; border:1pt solid #999999; background-color:#CCFFCC; text-align:center;" class="font8text">Y</span>
<span style="text-align:left; white-space:nowrap;" class="font8text">DINNIMAN</span>
</div>

<div align="left" style="background-color:#F8F8FF;">
  <span style="width:15px; padding:1px; border:1pt solid #999999; background-color:#FFCCCC; text-align:center;" class="font8text">N</span>
  <span style="text-align:left; white-space:nowrap;" class="font8text">EARLL</span>
</div>

3 个答案:

答案 0 :(得分:1)

使用DOM解析器,例如:http://simplehtmldom.sourceforge.net/

如上所述(a painless amount of times)。正则表达式不是解析HTML的好方法。实际上,你无法用Regex真正解析HTML。 HTML不是任何形式的常规。你只能提取位。而且(在大多数情况下)仍然是非常不可靠的数据。

最好使用DOM解析器。因为解析器将HTML解析为文档,所以更容易遍历。

示例:

include_once('simple_html_dom.php');

$dom = file_get_html('<html>...');

foreach($dom->find("div.head div.fact p.fact") as $element)
    die($element->innertext);

答案 1 :(得分:0)

我认为你最好使用strpossubstr相互简洁。

示例:

$str = <insert your string here>; // populate data
$_find = 'class="font8text">'; // set the search text
$start = strpos($str,$find) + strlen($_find); // find the start off the text and offset by the $needle
$len = strpos($str,'<',$start) - $start; find the end, then subtract the start for length
$text = substr($str,$start,$len); // result

答案 2 :(得分:0)

这样就可以了:

/class="font8text">N.*?class="font8text">(.*?)</m

EARLL将出现在第一个匹配组中。试试Rubular

相关问题