Solved

Replacing words with preg_replace?

Posted on 2009-06-29
4
314 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

0
Comment
Question by:Azurewave
  • 3
4 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24737657
Are all the words you want to replace bounded by whitespace on both sides?
0
 
LVL 1

Author Comment

by:Azurewave
ID: 24737782
They would be


The story is about Amy and something else.


In that sentence I would be trying to replace Amy.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24738349
You can use a regular expression to handle this.  I am rarely 100% certain of all the REGEX syntax, but this works and will get you pointed in the right direction.  

Man Page Here:
http://en.wikipedia.org/wiki/Regular_expression

BTW, there is a "Regular Expressions" Zone here at EE.

Best regards, ~Ray
<?php // RAY_temp_regex2.php

error_reporting(E_ALL);

echo "<pre>\n";
 

$find_and_replace = array(

    'Jim'           => 'cake',

    'John'          => 'james',

    'Kevin Smith'   => 'bill',

    "Sarah's Store" => "Bob's Auto",

    "Jim's Store"   => "Jen's Auto"

   );
 

$text = "Jim and John were on their way to meet Kevin Smith at Sarah's Store.";
 

// PAD TEXT WITH BLANKS

$new_text = ' ' . $text . ' ';
 

// ITERATE OVER THE FIND AND REPLACE ARRAY

foreach ($find_and_replace as $find => $replace)

{

// CONSTRUCT A REGEX

    $regex = '';

    $regex .= '/(\s+)';          // STARTS WITH WHITESPACE CHARACTER(S)

    $regex .= '(' . $find . ')'; // THE THING WE WANT TO FIND

    $regex .= '(\s+|[^\w]+)/';   // ENDS WITH WHITESPACE OR NON-WORD CHARACTER(S)
 

// EXECUTE THE REGEX-CONTROLLED REPLACEMENT

    $new_text = preg_replace($regex, "$1$replace$3", $new_text);
 

// WATCH THE PROGRESS FOR EACH REPLACEMENT

    var_dump($find);

    var_dump($replace);

    var_dump($regex);

    var_dump($new_text);

    echo "\n\n";

}
 

// REMOVE THE PADDING

$new_text = trim($new_text);

var_dump($new_text);

?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 25453534
Thanks for the points, ~Ray
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now