0

Hello again Stackoverflow!

<strong>Potato</strong>

preg_match("'<strong>(.*?)</strong>'si", $source, $match);

This will return Potato, simple regex. But here's the challange. I now have a setup like this that I want to match:

<strong>Potato</strong> Carrot
<strong>Apple</strong> Pear
<strong>Lemon</strong> Citron

And I would like to have that in an array like so:

(
    'Potato' => 'Carrot',
    'Apple' => 'Pear',
    'Lemon' => 'Citron'
)

But I have no idea how I could approach this. Could you guys help me?

Thanks in advance!

Thew
  • 15,789
  • 18
  • 59
  • 100

1 Answers1

1

You can use this:

preg_match("'<strong>(.*?)</strong>\s*([^<]*)'si", $source, $match);

See demo

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Thanks! It works, kinda though. What if `Carrot`, `Pear` and `Citron` are links? They don't seem to get parsed now. – Thew Sep 02 '13 at 08:37
  • @Thew. If you are dealing with parsing full blown HTML, you should better use an HTML parser instead. You are going to get headache fighting with regex and HTML at the same time. [Time to read this post](http://stackoverflow.com/q/1732348/1679863) – Rohit Jain Sep 02 '13 at 08:39
  • I'm aware of HTML parsing, but using Simple HTML dom (http://simplehtmldom.sourceforge.net/) I don't seem to get what I want. – Thew Sep 02 '13 at 08:40
  • @Thew. It might be possible, but it will take a lot of effort, which will not be worth at the end. I myself am not much experienced with HTML parsers, so I can't help you with that much. – Rohit Jain Sep 02 '13 at 08:50
  • Doesn't matter. Either way thanks for the push in the right direction. – Thew Sep 02 '13 at 09:06