Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Replace words across entire document from list
Old 01-20-2009, 05:52 PM Replace words across entire document from list
NullPointer's Avatar
Will Code for Food

Posts: 2,787
Name: Matt
Location: Irvine, CA
Trades: 0
I've constructed a fairly long list of terms formatted as:

Code:
Target , Replace
Target , Replace
Target , Replace
.
.
.
What would be the most efficient way to replace all instances of Target with Replace within the entire body of a document?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
 
Register now for full access!
Old 01-20-2009, 06:12 PM Re: Replace words across entire document from list
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,520
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Open it in a text editor with search and replace capabilities
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-20-2009, 06:58 PM Re: Replace words across entire document from list
NullPointer's Avatar
Will Code for Food

Posts: 2,787
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by chrishirst View Post
Open it in a text editor with search and replace capabilities
That would work if the document were static but I'm talking about the content of a .php page. The content is being pulled from a database and I'm trying to write a function that reads in words from a list and replaces all instances of those words with a corresponding replacement word. So if my word list contains:

Code:
foo , bar
internet , interweb
webmaster-talk , wmt
and the document at one point in type contains the text:

Code:
I use the internet to visit webmaster-talk.... foo
This should be converted to

Code:
I use the interweb to visit wmt... bar
The list is several hundred lines long so I was curious as to the most efficient way to accomplish this in javascript.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 01-20-2009, 07:21 PM Re: Replace words across entire document from list
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,520
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
PHP would be the answer NOT javascript

PHP Code:
$oldwords = array("foo","internet","webmaster-talk");
$newwords = array("bar","interweb","wmt");

$outstring str_replace($oldwords,$newwords,$instring); 
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Replace words across entire document from list
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13260 seconds with 12 queries