homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

ereg replace for items not in <>

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

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)

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)

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)

You could always strip_tags [php.net] first.


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

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)

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]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved