We help IT Professionals succeed at work.

Regex Help

Delerium1978
Delerium1978 asked
on
Hi,

I'm trying to figure out a regexp to extract the number (377) from this snippet in php:

<tr><th>Skill Index</th><td>377</td></tr>

Open in new window


The full html is attached.

I'm terrible at doing preg_match - it just won't sit right in my head :(

James
html.txt
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Would this not suffice?

<td>(\d+)</td>

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
In looking at your attachment, you probably want something more like:

<td>([\d,]+)</td>

Open in new window


or perhaps

<td>([^<]+)</td>

Open in new window

Author

Commented:
Don't think so, the html file attached has lots of <td> </td> in it.

The actual string:

<tr><th>Skill Index</th><td>

Open in new window


..right before the number is always a unique instance in the html file.
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:
This works for me:

preg_match("#<th>Skill Index</th><td>([\d,]+)</td>#", $source_string, $matches);
print $matches[1];
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Ah.

<th>Skill Index</th>\s*<td>([\d,]+)</td>

Open in new window

Author

Commented:
Thank you both for your help.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.