2

I have a variable ($page) that contains HTML code as follows:

<html>
    <head>head</head>
    <body>
        <strong>12</strong>
        recommendations 
        <br />
        <strong>50</strong>
        connections
    </body>
</html>

Now I want to get the number of connections (in this case it's 50) with a regular expression in PHP, so I use this:

preg_match('#<strong>(.*)</strong>#', $page, $connections)

But this gets 12, not 50. How can I solve this problem?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
stevey
  • 1,137
  • 4
  • 21
  • 30
  • Do you want both values or always the 2nd one? Always the last one? Always the one before "connections"? (Also, please see http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags - there are better/easier ways to handle HTML.) –  Oct 22 '12 at 16:12

2 Answers2

4
preg_match('/<strong>([0-9]*)<\/strong>\s+connections/', $page, $connections);

echo $connections[1]; // 50
CAMason
  • 1,122
  • 7
  • 13
  • 1
    +1 Because, if this must be an ugly regular expression - which I am opposed to, but that's another issue - it is best to make it specific to the problem. –  Oct 22 '12 at 16:13
2

With the caveat that Regular Expressions should not be used to parse HTML, take a look at preg_match_all(). If the HTML follows that structure, you are interested in the second match.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174