Jump to content

Beginner Regex. Change Adam John to x y.


mac_gabe

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.