473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Replacing words in a string

If I have a textfile of words, each word on one line:

dog
cat
viagra

for instance, and I get them into an array, how then can I replace those
words in a string with something else:

"The quick dog took viagra and the lazy cat was unhappy."

I want it to say:

"The quick ### took ### and the lazy ### was unhappy."

I've been banging my head against this, with foreach (both forms) and
rtrim, and preg_replace, and str_replace, and I can't get it to replace.

I don't need to see code, just a general "algorithm" would be useful.

Thanks.
--
Looks like more of Texas to me.
.... Arizona, where the nights are warm and the roads are straight.
Jul 17 '05 #1
2 1827
Warren Oates wrote:
If I have a textfile of words, each word on one line:

dog
cat
viagra

for instance, and I get them into an array, how then can I replace those
words in a string with something else:

"The quick dog took viagra and the lazy cat was unhappy."

I want it to say:

"The quick ### took ### and the lazy ### was unhappy."

I've been banging my head against this, with foreach (both forms) and
rtrim, and preg_replace, and str_replace, and I can't get it to replace.

I don't need to see code, just a general "algorithm" would be useful.

Thanks.

foreach ($badwordsarray as $val) {
str_replace ("$val" , "###", $sentence);
}

I think that would do the trick...
Jul 17 '05 #2
Warren Oates wrote:
<snip>
"The quick dog took viagra and the lazy cat was unhappy."

I want it to say:

"The quick ### took ### and the lazy ### was unhappy." <snip> I don't need to see code, just a general "algorithm" would be useful.


Sorry, but you really do not need an algorithm:
str_replace() can take an array for a parameter.

$x = str_replace(
array("dog", "cat", "viagra"),
"###",
"The quick dog took viagra and the lazy cat was unhappy.");
--
Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
== ** ## !! !! ## ** ==
TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
bypass the spam filter. I will answer all pertinent mails from a valid address.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christopher R. Barry | last post by:
I need to search and replace multiple words in one pass of an input stream or string. For example, given the input: "The quick brown fox jumped over the lazy dog's back" and given the...
3
by: dumbledad | last post by:
Hi All, I'm confused by how to replace a SELECT statement in a SQL statement with a specific value. The table I'm working on is a list of words (a column called "word") with an index int...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
0
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
2
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
6
by: Eli Silverman | last post by:
I am using a c# utility to create a Java function on our web pages. I am utilizing an object we created to retrieve a comment field from our SQL database and have the system throw the desired alert...
0
by: Lakhi | last post by:
hi frnds, I need small help in String replacement I need to replace the text using replceAll() with Case-Insensitve . Is there any regular expression for this? i have this expression ...
7
by: aine_canby | last post by:
Hi, Im totally new to Python so please bare with me. Data is entered into my program using the folling code - str = raw_input(command) words = str.split() for word in words:
7
by: DarthBob88 | last post by:
I have to go through a file and replace any occurrences of a given string with the desired string, like replacing "bug" with "feature". This is made more complicated by the fact that I have to do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.