mac_gabe Posted April 25, 2013 Share Posted April 25, 2013 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. 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 str_replace 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! 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
Archived
This topic is now archived and is closed to further replies.