Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

ereg replace for items not in <>



12:26 am on Jul 31, 2009 (gmt 0)

10+ Year Member

Since my user name should really be "I Suck At Regex", I'm wondering what the regex for an ereg replace would be needed to catch a string that is not contained in any form of html tag, so basically not within < >.

e.g. It should find and replace John Doe in:

"John Doe was born on..."

but not find/replace when John Doe's in any tag for example:

<img src="/jd.jpg" alt="John Doe at Webmasterworld" />

Thanks for any suggestions.


12:48 am on Jul 31, 2009 (gmt 0)

5+ Year Member

just make the "John Doe was born on..." a variable:

 $john = "John Doe was born on..."; 

and call the variable $john as your subject.


1:00 am on Jul 31, 2009 (gmt 0)

10+ Year Member

I'm sorry, but I think you missed my point. I'm looking for a regular expression (regex) pattern that will find instances of a search text in a string as long as that search text is not contained within <>.


7:18 pm on Aug 4, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

You could always strip_tags [php.net] first.


8:03 pm on Aug 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

If the goal is to end up with an updated HTML page, I'd be a bit more brute-force in my approach because I believe that strip_tags just leaves you with the page content and no simple way to restore the stripped tags once you've made your changes.

I'd start by extracting the content between the <body> tags, followed by replacing all HTML tags with sequentially-numbered markers, storing the tags along the way so they could be restored by replacing the markers. (You might have to special-case <style></style> and <script></script> sections to avoid altering them.) That would make global changes to the document content via regexp simple(r). And once that's done, you just restore the tags that you removed in the first pass, and re-insert the results back into the <body></body>.


8:34 pm on Aug 4, 2009 (gmt 0)

10+ Year Member

Thanks for the tips. Someone at another site suggested this:

$str = preg_replace('/John Doe(?![^<>]*+>)/i', $new_name, $str);

It seems to work.

[edited by: eelixduppy at 3:09 am (utc) on Aug. 5, 2009]
[edit reason] disabled smileys [/edit]


Featured Threads

Hot Threads This Week

Hot Threads This Month