e111982 Posted November 23, 2009 Share Posted November 23, 2009 Hi. I have a replace problem. I have strings as: $old1="danger"; $old2="non-danger"; $new1="dangerous"; $new2="safe"; $str="A typical explosives factory is divided into two parts: the 'non-danger' and 'danger' areas."; echo preg_replace("~\b$old\b~i", $new, $str); And I want an output as: A typical explosives factory is divided into two parts: the 'safe' and 'dangerous' areas. It gives as: A typical explosives factory is divided into two parts: the 'non-dangerous' and 'dangerous' areas. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 It's because \b matches a word boundary, which checks if the character before/after is [^a-zA-Z0-9_]. If you wish to match - as not being a word boundary you will probably have to use your own character set to emulate \b. Untested, but something like... "#[^a-z-A-Z0-9_-]$old[^a-z-A-Z0-9_-]#" Quote Link to comment Share on other sites More sharing options...
e111982 Posted November 23, 2009 Author Share Posted November 23, 2009 This code is not working. Where is the error? Thanks.I am new. $old="danger"; $new="danger2"; $str="A typical explosives factory is divided into two parts: the 'non-danger' and 'danger' areas."; echo preg_replace("#[^a-z-A-Z0-9_-]$old[^a-z-A-Z0-9_-]#", $new, $str); Quote Link to comment Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 My bad, the dollar from the variable is a meta character in Regular Expressions, this should fix it. "#[^a-z-A-Z0-9_-]{$old}[^a-z-A-Z0-9_-]#" Quote Link to comment Share on other sites More sharing options...
e111982 Posted November 23, 2009 Author Share Posted November 23, 2009 Thanks. this code is working however Is there a way to keep the punctuation marks before and after the words? In this way the comma,dot,quotation marks etc. disappear. Quote Link to comment Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 You'd probably have to replace the character sets with look ahead and look behind assertions. I've never used them so I can't say exactly how it's done, I'll have a play around and see what I can work out. Quote Link to comment Share on other sites More sharing options...
e111982 Posted November 23, 2009 Author Share Posted November 23, 2009 Thanks. I add the characters but not working. Quote Link to comment Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 Hmm... I think I got it, if I did, it turns out it's fairly simple. #(?<![a-zA-Z0-9_-]){$old}(?![a-zA-Z0-9_-])#" I'm sure if I'm too far off base somebody with a bit more experience will chime in, but at a quick test it seems to work. Quote Link to comment Share on other sites More sharing options...
e111982 Posted November 23, 2009 Author Share Posted November 23, 2009 thanks a lot. I think there is no problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.