Solved

Replace words in string

Posted on 2009-04-01
3
329 Views
Last Modified: 2012-05-06
I have documents, and there are certain words in those documents that I want to replace with a new word.

for example, I want to replace all instances of

dog with cat.

I know I can do a str_replace and this will do it for me, however I run into an issue where it would replace "hotdog" with "hotcat", which is something I do not want to do.  So str_replace is not an option.

Some of the words, also have an apostrophe in them, so I would like to take those into account as well.
0
Comment
Question by:Azurewave
  • 2
3 Comments
 
LVL 3

Expert Comment

by:FirestormX
ID: 24043668
You would want to use a regular expression (regex), and the preg_replace function.

We basically make an array of the things we want to find, then run them through preg_replace.
You may want to do some research into regex, but basically it uses characters to represent things; for example, "?" means "if there is 1 or 0 instances of the previous character". So if you are looking for something like
"it's" and "its", then you could do a search for "it'?s" (it will match strings with both the ' and without).

I don't have an incredibly good grasp on regex, but I believe \b defines a word boundary between anything encased in it. So if you were searching "\bit'?s\b" in "it's like a little bit of its own flavour", it would find "it's", and "its", but not the "its" in "bits".

Hopefully I explained that properly. You could take a look at http://www.regular-expressions.info/wordboundaries.html (or http://www.regular-expressions.info/wordboundaries.html for more info on the \b tag)
0
 
LVL 3

Expert Comment

by:FirestormX
ID: 24043746
Whoops, I forgot to show you that in practise. In PHP, the regex values must be between / tags to signify they are regex.  In the example we'll replace "it's", and "its" (by searching "it'?s"), "dog", and both "cat" and "cats" (by searching "cats?").
I also forgot to note that anything in the searching expression can be back-tracked by putting it in brackets. That is to say, if you search for "cat(s?)", you can reference what's inside of the brackets as $1 (or whatever number the bracket is at).

I've provded an example below, but I'm at work right now, so unfortunately I don't have time to test it, so it may need some tweaking.
$search = array('/\bit'?s\b/', '/\bdog\b/', '/\bcat(s?)\b/'); //What to replace

$replace = array('it is', 'cat', 'dog$1');

$old_string = "$_POST['text']";

$new_string = preg_replace($search, $replace, $old_string);

Open in new window

0
 
LVL 13

Accepted Solution

by:
marchent earned 500 total points
ID: 24044155

$content = 'dog my dog hotdog dog\'s place the dog123 67dog77 dog and only dog';

$content = preg_replace('/(\W)dog(\W)/i', '$1cat$2', $content);  ## repalce from middle

$content = preg_replace('/^dog(\W)/i', 'cat$1', $content);  ## repalce from first

$content = preg_replace('/(\W)dog$/i', '$1cat', $content);  ## repalce from last

print "$content\n";

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
count download link and run update query 9 55
Create a Select Query and Populate a Table 3 44
simplest php form 3 59
Whether to use true/false, yes/no or 0/1 11 51
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 look for a specific file type in a local or remote server directory using PHP.

910 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

22 Experts available now in Live!

Get 1:1 Help Now