matching dynamic text
Posted: Sat May 03, 2008 3:07 am
I'm trying to match text that can possibly change. The data I am trying to match is inside an html tag:
This regex will match the data inside the > <
The problem with that is, I need to know the numbers so I can preform arithmetic on them. *Something* like the regex below will work in the case above:
However, that assumes there are always three numbers separated by three strings, when in reality there are tree cases that can happen:
I'd like a regular expression that can account for the three possibilities. Moreover, [if possible/not necessary] I'd like to have all the numbers to keep their index constant.
Case One should end up like:
$stats[0] = x;
$stats[1] = y;
$stats[2] = z;
Case Two:
$stats[0] = 0;
$stats[1] = y;
$stats[2] = z;
Case Three
$stats[0] = 0;
$stats[1] = 0;
$stats[2] = z;
That way, index 0 will always be the number of days, index 1 will be the number of hours, and index 2 will be the number of minutes. If course that is the ideal situation I'd like, but I wouldn't mind checking the size of the array [if 3 => case 1,;if 2 => case 2; if 1 => case 3]
Thanks
Code: Select all
<span id="Stats_lbl1">5 days 12 hours 4 minutes</span>Code: Select all
<?php
$content = '<span id="Stats_lbl1">5 days 12 hours 4 minutes</span>';
preg_match('#(?<=<span id="Stats_lbl1">).*?(?=</span>)#', $content, $stats);
?>Code: Select all
preg_match('#(?<=<span id="Stats_lbl1">)(\d+)(.*?)(\d+)(.*?)(\d+)(.*?)(?=</span>)#', $content, $stats);Code: Select all
//Case 1
$content = '<span id="Stats_lbl1">x days y hours z minutes</span>';
//Case 2
$content = '<span id="Stats_lbl1">y hours z minutes</span>';
//Case 3
$content = '<span id="Stats_lbl1">z minutes</span>';Case One should end up like:
$stats[0] = x;
$stats[1] = y;
$stats[2] = z;
Case Two:
$stats[0] = 0;
$stats[1] = y;
$stats[2] = z;
Case Three
$stats[0] = 0;
$stats[1] = 0;
$stats[2] = z;
That way, index 0 will always be the number of days, index 1 will be the number of hours, and index 2 will be the number of minutes. If course that is the ideal situation I'd like, but I wouldn't mind checking the size of the array [if 3 => case 1,;if 2 => case 2; if 1 => case 3]
Thanks