1

Possible Duplicate:
RegEx match open tags except XHTML self-contained tags

I am new with the regular expression and i want to fetch the data(Address) between the using regular expression here is my code

<th align="left" valign="top">Billed To:</th>
<th align="left" valign="top">Shipped To:</th>
</tr>
<tr>
<td height="107" valign="top">
Logmet<br>
Wayne Rankin<br>
1400 Lake Bluff Cove <br>
Round Rock TX US 78665<br>
512-924-5437<br>
[email protected]<br></td>
<td valign="top">
Logmet<br>
Wayne Rankin<br>
1400 Lake Bluff Cove <br>
Round Rock TX US 78665<br>
512-924-5437<br>
[email protected]    </td>
</tr>

Does anyone know how i can get the address between these tags ... Thanks in advance

4
  • Similar thread stackoverflow.com/questions/9253027/…
    – ka_lin
    Nov 20, 2012 at 10:38
  • am try to get address between <td height="107" valign="top"></td> that tag
    – Fungsuk
    Nov 20, 2012 at 10:38
  • Use an HTML Parser. Regex is not the right tool for this job. Nov 20, 2012 at 10:53
  • ya i tried with many expressions but i don't get the data .. :)
    – Fungsuk
    Nov 20, 2012 at 10:56

2 Answers 2

0

As some said already a regex isn't the best tool to use here... but if you MUST use it:

$chars=array("\r\n", "\n", "\r");
$html=str_replace($chars, '', $html);
preg_match_all('/<td.*?>(.*?)<br>(.*?)<br>(.*?)<br>(.*?)<br>(.*?)<br>(.*?)<br><\/td>/', $html,$hits);
print_r($hits);

That one gets you EVERYTHING on each td, split in diferent array positions based on the br. If you just want to get some of them, remove the () of the ones you don't need

3
  • Thanks for your response.. am try with that code but its print blank array ...
    – Fungsuk
    Nov 20, 2012 at 11:16
  • totally true! edited my code. The problem are the \r\n or \n that may appear and the .* is unnable to read. If you remove them before parsing it works ^^.
    – Naryl
    Nov 20, 2012 at 11:27
  • thanks Mate ... its working now. It show now all td tab data .. once again thanks alot
    – Fungsuk
    Nov 20, 2012 at 11:48
0

I don't know the regular expression approach but when it comes to parsing html I use this http://simplehtmldom.sourceforge.net/

It's nice and clean and works well for me.

Not the answer you're looking for? Browse other questions tagged or ask your own question.