Xriuk
Xriuk

Reputation: 392

PHP RegEx create an associative array based on pattern

I have this string:

$str = '<td class="title">Genre:</td><td class="detail"><a href="/en-us/store/browse/action">   Action   </a></td></tr>';
$str2 = '<td class="title">Release Date:</td><td class="detail">   January 13 2009  </td></tr>';

I need to get an associative array structured like this:

$arr[$title] = $detail;

My problem is not the pattern itself, but how to create an array. Thanks for any help.

Upvotes: 0

Views: 137

Answers (1)

georg
georg

Reputation: 215009

If your regexp is like this

$re = '~<td class="title">(.+?):</td><td class="detail">(.+?)</td>~';

then you can simply do

preg_match($re, $str, $matches);
$arr[$matches[1]] = $matches[2];

Do note however, that regexes is not the best way to parse html.

Upvotes: 1

Related Questions