regexp to find this pattern
PHP => How can i search through this string in such a way that when i have class="font8text">N</span>'
to give me 'EARLL' which is in the next <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; bord开发者_如何转开发er: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>
Use a DOM-parser like: http://simplehtmldom.sourceforge.net/
As mentioned (a painless amount of times). Regex is not a good way to parse HTML. Actually, you can't really parse HTML with Regex. HTML is not regular in any form. You can only extract bits. And that's still (in most cases) very unreliable data.
It's better to use a DOM-parser. Because a parser that parses the HTML to a document, makes it easier to traverse.
Example:
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);
I think you're better off using strpos and substr succinct with each other.
Example:
$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
This would do it:
/class="font8text">N.*?class="font8text">(.*?)</m
EARLL
would be in the first match group. Try it on Rubular.
精彩评论