0

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; 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>
El Yobo
  • 14,823
  • 5
  • 60
  • 78
mihai
  • 1
  • 6
    Use an HTML parser -> Find an element with the right class and contents -> retrieve the contents of the next successive element – Anon. Dec 08 '10 at 21:57
  • Weird, the question claims to be about PHP, but is tagged jquery instead. It can't be both, can it? – Jason Orendorff Dec 08 '10 at 22:37
  • check this http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – NullPoiиteя Dec 30 '12 at 06:32

3 Answers3

1

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);
Community
  • 1
  • 1
Robin Orheden
  • 2,714
  • 23
  • 24
  • +1 for dom parser ...instead of [dam regx for this](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – NullPoiиteя Dec 30 '12 at 06:32
0

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
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
0

This would do it:

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

EARLL would be in the first match group. Try it on Rubular.

detunized
  • 15,059
  • 3
  • 48
  • 64