0

I have the following HTML:

<tr valign="top">
        <td>Name:</td>
        <td>John Doe</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr valign="top">
        <td>Address:</td>
        <td>71 view st, Fitchburg, MA 01420</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr valign="top">
        <td>Phone:</td>
        <td>978-345-5391</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
        <td>Email:</td>
        <td>[email protected]</td>
</tr>

I want to capture the values of Name, Phone and Email within the same REGEX pattern, something like:

$pattern = "/Name:<\/td>\s*<td>(.*)<\/td>.*Address:<\/td>\s*<td>(.*)<\/td>.*card ([0-9].*)<\/td>/m";

If I try separately, like:

$pattern = "/Name:<\/td>\s*<td>(.*)<\/td>/m";
$pattern = "/Phone:<\/td>\s*<td>(.*)<\/td>/m";
$pattern = "/Email:<\/td>\s*<td>(.*)<\/td>/m";

is okay. Maybe I don't understand how REGEX works, isn't possible to have more matches in a single pattern?

8
  • Parsing html markup by means of a regular expression typically is not a good approach. It is very unreliable and complex. Take a look at a DOM parser instead. Those are the right tool for such task. Commented Nov 20, 2016 at 17:12
  • I know, I can't use DOM because my file doesn't have any full HTML tags, just TABLE and so on, so I can't access DOM. Commented Nov 20, 2016 at 17:15
  • That does not mean you cannot use a DOM parser! Simply embed that table definition into a minimalistic HTML framework. Commented Nov 20, 2016 at 17:16
  • 1
    1. You can do it with a Regex. You need an s flag at the end so that it will count newlines as white space. 2. Your regex does not match. 3. It is easier to use # instead of / as your regex delimiters since you have / in your haystack. Commented Nov 20, 2016 at 17:26
  • 1
    Why did people downvote the question? This was a straight forward question. Commented Nov 20, 2016 at 19:12

1 Answer 1

2

Example showing multiline regex. 1. Not best solution to problem but illustrates ability. 2. Not the best regex but the point is the s flag at the end.

<?php
$html = <<<EOL
<tr valign="top">
        <td>Name:</td>
        <td>John Doe</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr valign="top">
        <td>Address:</td>
        <td>71 view st, Fitchburg, MA 01420</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr valign="top">
        <td>Phone:</td>
        <td>978-345-5391</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
        <td>Email:</td>
        <td>[email protected]</td>
</tr>
EOL;

$pattern = "#Name:</td>.*<td>(.*?)</td>.*Address:</td>.*<td>(.*?)</td>.*Phone:</td>.*<td>(.*?)</td>.*Email:</td>.*<td>(.*?)</td>#s";

if(preg_match($pattern, $html, $matches)) {
    printf("Name: %s\n", $matches[1]);
    printf("Address: %s\n", $matches[2]);
    printf("Phone: %s\n", $matches[3]);
    printf("Email: %s\n", $matches[4]);
}


?>

Yields

Name: John Doe
Address: 71 view st, Fitchburg, MA 01420
Phone: 978-345-5391
Email: [email protected]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.