mac_gabe Posted April 25, 2013 Share Posted April 25, 2013 (edited) I think I may have known how to do this once but I've definitely forgotten now. Please help. I have a text with many instance of "Adam" and "John" in it. In a single find-and-replace (I don't want to use 2 passes) I want to replace all instance of "Adam" with "x", and "John" with "y". In fact the actual example is a little more complex, but this will solve it. I'm struggling with this: $search="@(Adam)|(John)@"; $replace="@x|y@"; $text = preg_replace($search, $replace, $text); I know there should be a back reference - something like $1 or $2 in the replace term probably; but that's not going to produce the letters x or y. So I'm stuck. Thanks for any help. Edited April 25, 2013 by mac_gabe Quote Link to comment https://forums.phpfreaks.com/topic/277315-beginner-regex-change-adam-john-to-x-y/ Share on other sites More sharing options...
Jessica Posted April 25, 2013 Share Posted April 25, 2013 (edited) str_replace Edited April 25, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/277315-beginner-regex-change-adam-john-to-x-y/#findComment-1426615 Share on other sites More sharing options...
mac_gabe Posted April 25, 2013 Author Share Posted April 25, 2013 Thanks Jessica! Quote Link to comment https://forums.phpfreaks.com/topic/277315-beginner-regex-change-adam-john-to-x-y/#findComment-1426625 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.