We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Replacing words with preg_replace?

Azurewave
Azurewave asked
on
Medium Priority
410 Views
Last Modified: 2012-05-07
I am trying to figure out a way to replace the following arrays with the ones below.  The reason I don't use str_replace is because when I have similar words together it breaks them, for example "ana" would get replaced in "ananda", even though I just wanted the word "ana" not to interfiere with the other.

Some of my words that i wan't replace also have apostrophe's, so how would I take care of that?
<?php
 
$find = array(
    'Jim',
    'John',
    'Kevin Smith',
    "Sarah's Store",
    "Jim's Store"
);
$replace = array(
    'cake',
    'james',
    'bill',
    "Bob's Auto"
    "Jen's Auto"  
);
 
?>

Open in new window

Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Are all the words you want to replace bounded by whitespace on both sides?

Author

Commented:
They would be


The story is about Amy and something else.


In that sentence I would be trying to replace Amy.
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Thanks for the points, ~Ray
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.