cedivad
cedivad

Reputation: 2644

Match everything except given string

Ok, it's 2 hours that i try. It's time to ask someones here at stackoverflow.

I have this source html:

<div class="post_main">
Post Content (has HTML)
</div><div class="post_email">[...]
Other Stuff
<div class="post_main">
Post Content (has HTML)
</div><div class="post_email">[...]
Other Stuff
<div class="post_main">
Post Content (has HTML)
</div><div class="post_email">[...]

And so on. I want to add "Something" before every Post Content.

To do so i'm using preg_replace, but it won't work. Since that i'm actually using

'|<div class="post_main">(.*)</div><div class="post_email">|s

As regex, the

"</div><div class="post_email">"

is eaten from the (.*) and i have only one substitution instead of 3.

Now, how can i get this preg match:

Match everthing included newline, but exclude a given string (in this case:

"</div><div class="post_email">")

?

Thank you a lot.

Upvotes: -1

Views: 271

Answers (2)

Scorpil
Scorpil

Reputation: 1526

Answer from mistabell is complitely appropriate for your case. If you still want to do it your way - just add one little '?' after the '*' sign. I mean like this:

'|<div class="post_main">(.*?)</div><div class="post_email">|s

It calls lazy quantifier, and it makes your regexp less greedy.

Upvotes: 2

user479947
user479947

Reputation:

function addSomething($str,$add) {
  $find = '/(<div class=\"post_main\">)/';
  $replace = '$1' . $add;
  return preg_replace($find, $replace, $str);
}

Upvotes: 2

Related Questions