-5

Possible Duplicate:
How can I replace strings NOT within a link tag?

Hi is there anyone can help me with regular expression

This is the words that i want to replace

$text = '<a href="test-pregnancy.net">pregnancy<a> week by week, some time pregnancy can be very easy';
$search = 'pregnancy';
$replace = '<a href="google-pregnancy.net">pregnancy</a>';

print preg_replace('/('.$search.')/', $replace, $text);

is there any regular expression that except for words that contain inside a link? only replace words that not contain inside a link

I want it would be like this

$text = '<a href="test-pregnancy.net">pregnancy</a> week by week, some time <a href="test-pregnancy.net">pregnancy</a> can be very easy';

How if the condition like this.

$text = '<a href="test-pregnancy.net" title="welcome pregnancy people">pregnancy<a> week by week, some time pregnancy can be very easy';

This will also replace pregnancy in the title of href. Is there any another idea. I want a regex that using exception inside a tag.

Community
  • 1
  • 1
Mason Anthony
  • 21
  • 1
  • 3
  • 8
    That is some really weird sample text. – BoltClock Jan 06 '12 at 01:16
  • 3
    No, there is no regular expression that can do that. At least not if your HTML is much more complex than one anchor followed by some text. – Paul Jan 06 '12 at 01:17
  • Yes, that's doable. Came up before, use the search function. – mario Jan 06 '12 at 01:18
  • 1
    @BoltClock why? because "pregnancy" is spelled correctly, but "verry" isn't? :^P – Maarten Bodewes Jan 06 '12 at 01:20
  • 2
    @BoltClock while you are busy, maybe we should replace the rather woman-unfriendly sample text and change "exeption" in the title to "exception" - personally, I think this post amounts to trolling though, and I don't think this question has not been answered before – Maarten Bodewes Jan 06 '12 at 01:34
  • @owlstead: If you find a dupe, go ahead and vote it as such. A little less work for everyone :) – BoltClock Jan 06 '12 at 01:47
  • Duplicate of: http://stackoverflow.com/questions/2165381/how-can-i-replace-strings-not-within-a-link-tag and http://stackoverflow.com/questions/4044812/regex-domdocument-match-and-replace-text-not-in-a-link – Dan Blows Jan 06 '12 at 10:05

2 Answers2

0

replace:

$search =  'pregnancy';

with:

$search =  ' pregnancy ';
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
-1

You should use a HTML parser and find the text that is not part of an A element. See: http://simplehtmldom.sourceforge.net/

Mircea Vutcovici
  • 1,894
  • 19
  • 27