-2

I'm very new at regular expressions: I want to preg_match all elements in a html dom, that has a data-editable attribute. All other attributes of those elements should be matched also, so i can reuse them later:

<div class="teaser" id="teaser" data-editable><p>Content</p></div>

After matching i want those elements with data-editable attribute to have specific css classes and add another element inside. So only block-level parents should be matched.

<div class="teaser editable" id="teaser"><button>edit</button><p>Content</p></div>

Here's what i've got:

<(div|p).*(data-editable).[^>]+>(.*?)<\/\1>

I know, i'm totally wrong with that - this one matches also elements that does not have that data-editable attribute set because of that .+ inside. But how to match the different attributes without losing their values?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
chris.ribal
  • 435
  • 2
  • 10

2 Answers2

1

You shouldn't go through HTML with regex (as shown here). What you should do would be to use an HTML parsing framework, such as the PHP Simple DOM Parser to process your HTML pages.

According to their documentation, you can do what you want through this: $html->find("div[data-editable]", 0)->outertext

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
0

Since HTML isn't a regular language, you're better of using a DOM parser. Much easier, too

Robin Kanters
  • 5,018
  • 2
  • 20
  • 36