{% else-1 %}
Функцию str_replace() для PHP я использую достаточно часто. Она настолько удобна, что когда я пишу на JavaScript, иногда чувствую себя связанным по рукам и ногам. Поэтому пользуюсь вот этим чудным аналогом от Kevin van Zonneveld (функция принимает те же параметры и возвращает тот же результат).

                        
<?php
function str_replace ( search, replace, subject ) {
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Gabriel Paderni

if(!(replace instanceof Array)){
replace=new Array(replace);
if(search instanceof Array){//If search is an array and replace is a string, then this replacement string is used for every value of search
while(search.length>replace.length){
replace[replace.length]=replace[0];
}
}
}

if(!(search instanceof Array))search=new Array(search);
while(search.length>replace.length){//If replace has fewer values than search , then an empty string is used for the rest of replacement values
replace[replace.length]='';
}

if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
for(k in subject){
subject[k]=str_replace(search,replace,subject[k]);
}
return subject;
}

for(var k=0; k<search.length; k++){
var i = subject.indexOf(search[k]);
while(i>-1){
subject = subject.replace(search[k], replace[k]);
i = subject.indexOf(search[k],i);
}
}

return subject;

}
?>
0 30 0
Без комментариев...