JavaScript or PHP code needed to replace words

shanali4's Avatar, Join Date: Jun 2008
Newbie Member
Hi there I need a javascript code which searches a specific word on the whole page and replace it with a specific page. For Example I have a page which word google and i want to replace google with Yahoo.

Plz urgently needed this script. I prefer Javascript or PHP
0
shanali4's Avatar, Join Date: Jun 2008
Newbie Member
Plz help
0
oogabooga's Avatar
Ambitious contributor
I'm not exactly sure what you're looking for, but Javascript has regular expressions.
Here's an example of how to use them:
Code:
<script>
var re = new RegExp("google", "ig");
var s = "xxxxgooglexxxxxxxxxGOOGLExxx";
var r = s.replace(re, "yahoo");
document.write(r);
</script>
0
juresh's Avatar, Join Date: Sep 2008
Go4Expert Member
hey, where r you use this?
i got it.
i think you r making a social networking site (i think so) and you don't want, that somebody use wrong words on your site, if it used then you can replace with any specific word.

take this tut
http://www.tizag.com/javascriptT/jav...ng-replace.php
And
code
Code:
<script type="text/javascript">
function searchPage(word, replaceWord, tagName){
var elems = document.getElementsByTagName(tagName);
for(var i=0; i < elems.length; i++){
if(elems[i].innerHTML = word){
elems[i].innerHTML = replaceWord;
}
}
}
</script>
thanks