views:

85

answers:

2

how can i create a preg_match_all regex pattern for php to give me this code?

<td class="class2">&nbsp;</td>
<td class="class2" align="right"><span class="DarkText">I WANT THIS TEXT</span></td>

To get me the text inside the span class? thanks!

+3  A: 

You can use:

preg_match_all("!<span[^>]+>(.*?)</span>!", $str, $matches);

Then your text will be inside the first capture group (as seen on rubular)

With that out of the way, note that regex shouldn't be used to parse HTML. You will be better off using an XML parser, unless it's something really, really simple.

NullUserException
+1 for answer and pointing to best solution (parser). And for including the link to Bobince's famous answer.
alex
+1  A: 

It's tough to write Regexes that parse HTML perfectly. Instead, use an HTML parser like this one: http://simplehtmldom.sourceforge.net/. It is easy to use and I have recommended it on here several times.

SimpleCoder