0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

And what is the best way to parse html?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.