In the following string I need to extract only 2014-04-19 using php and regex with preg_match
...</abbr></td><td><abbr class="dtend" title="2014-04-19T00:00:00+00:00">19 Apr 2014</abbr></td></tr>...
The preg_match and regex string I use is:
preg_match("/class=\"dtend\" title=\"(.*)\"\>/i", $str,$str2);
The string I obtain is:
2014-04-19T00:00:00+00:00
After "T" numbers (time) are obviously random. I amm not very experienced, neither novice, but really can't fix the issue. Can you provide a suggestion/some help?
Your regex is incomplete. Change it to this:
"/class=\"dtend\" title=\"(.*)T.*\"\>/i"
That way, the capture group only captures up to the first T
.
you can use:
preg_match("/class=\"dtend\" title=\"([0-9\\-]+)[^\"]+\"\>/i", $str,$str2);
([0-9\\-]+)
will fetch any numberic symbols or -
(NOTE: because of multiple escaping you may need to use tree/four \
here)
[^\"]+
- skip all non "
symbols
Might use a lookahead to meet T
, there are many ways to do it:
$pattern = '/class="dtend" title="\K[^"]+(?=T)/i';
$out[0]
then.[^"]+
as charcters to be matched (+
one or more characters, that are not "
)For further regex info see the FAQ.
Why using a regelar expression on a known giving string ?
you should do something like this -
$var = '<td>T<abbr class="dtend" title="2014-04-19T00:00:00+00:00">19 Apr 2014</abbr></td></tr>';
$string_to_search = 'class="dtend" title="';
$start = strpos($var, $string_to_search);
$var = substr($var,$start+strlen($string_to_search),10);
It might look worst as regex do it in a simpler way but as long you know what you expecting you should always use simple string functions instead of regex.