0

Html code:

<div username="bob">
        <table>
                <tr>
                    <td>name</td>
                    <td>alex</td>
                </tr>
                <tr>
                    <td>location</td>
                    <td>London</td>
                </tr>
                <tr>
                    <td>id</td>
                    <td>123</td>
                </tr>
        </table>
    </div>

    <div username="apple">
        <table>
                <tr>
                    <td>name</td>
                    <td>david</td>
                </tr>
                <tr>
                    <td>id</td>
                    <td>321</td>
                </tr>
        </table>
    </div>

    <div username="pepper">
        <table>
                <tr>
                    <td>location</td>
                    <td>Bradford</td>
                </tr>
        </table>
    </div>

I want to select the location by username.
for example:
bob location = London,
apple location = return false,
pepper location = Bradford
I want to get with regex.

Jajarm
  • 41
  • 1
  • 1
  • 4
  • Why does the user, with the username of 'bob', have 'alex' as the specified name? And how are these tables being constructed, surely you have a reference to the location when you're creating them? – David Thomas Feb 04 '15 at 07:15
  • Have you tried something to do this? [Please read this](http://stackoverflow.com/help/how-to-ask) – Bowdzone Feb 04 '15 at 07:16
  • I only want to select by username. – Jajarm Feb 04 '15 at 07:18

1 Answers1

1
<div\s+username="pepper">(?:(?!<\/div>)[\s\S])*?location.*?\s+<td>([^<]*)

Try this.Change names as per your needs.See demo.Grab the capture or group.

https://regex101.com/r/vD5iH9/67

$re = "/<div\\s+username=\"pepper\">(?:(?!<\\/div>)[\\s\\S])*?location.*?\\s+<td>([^<]*)/i";
$str = "<div username=\"bob\">\n <table>\n <tr>\n <td>name</td>\n <td>alex</td>\n </tr>\n <tr>\n <td>location</td>\n <td>London</td>\n </tr>\n <tr>\n <td>id</td>\n <td>123</td>\n </tr>\n </table>\n </div>\n\n <div username=\"apple\">\n <table>\n <tr>\n <td>name</td>\n <td>david</td>\n </tr>\n <tr>\n <td>id</td>\n <td>321</td>\n </tr>\n </table>\n </div>\n\n <div username=\"pepper\">\n <table>\n <tr>\n <td>location</td>\n <td>Bradford</td>\n </tr>\n </table>\n </div>";

preg_match_all($re, $str, $matches);
vks
  • 67,027
  • 10
  • 91
  • 124