0

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?

laptou
  • 6,389
  • 2
  • 28
  • 59
Alby
  • 15
  • 6
  • 1
    Well, going by instinct, you could split by capital T and get the first chunk. What you need to do depends on all possible input scenarios that you want to handle. – Asad Saeeduddin Apr 11 '14 at 23:36

4 Answers4

3

Might use a lookahead to meet T, there are many ways to do it:

$pattern = '/class="dtend" title="\K[^"]+(?=T)/i';
  • \K resets the beginning of the match, which will be in $out[0] then.
  • Used [^"]+ as charcters to be matched (+ one or more characters, that are not ")

Test on regex101.com

For further regex info see the FAQ.

Community
  • 1
  • 1
Jonny 5
  • 12,171
  • 2
  • 25
  • 42
0

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.

rvighne
  • 20,755
  • 11
  • 51
  • 73
0

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

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
0

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.

Gal Sisso
  • 1,939
  • 19
  • 20