Shoutbox

replace_str - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: replace_str (/showthread.php?tid=83564)

replace_str by Zeelia on 05-06-2008 at 06:36 PM

Hi!
I have a problem, what i wanna do is that when you execute the command which i have created (/replaceM) it should replace words so what i want it to do is similar to the PHP function str_replace,

PHP

quote:
Description
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

i want it to search for i.e afk in the whole message im sending, for example:
/replaceM im going afk now, bye!
i want it to search for 'afk' in the sentence and replace the word 'afk' with '<Away From Keyboard>'
how can i do that? search for a word in a sentence and then replace it with another word?
RE: replace_str by MeEtc on 05-06-2008 at 08:24 PM

This should work well for you:
http://kevin.vanzonneveld.net/techblog/article/ja..._phps_str_replace/


RE: replace_str by Zeelia on 05-06-2008 at 08:36 PM

Thanks! That did work very well indeed.

*EDIT* nevermind, i saw how to make this in the link you sent! Thanks again

But now i have another issue, also in php you can use arrays to store information and now i need to store information in arrays but i dont know how to do it in javascript/Messenger Plus-script
so heres how i would to it in

PHP:

quote:
($ = declare variable)

$text = "OK! brb then. Or afk, i maybe take longer time than i think. so please dnd :)";
$array1 = array("afk", "brb", "dnd");
$array2 = array("<Away From Keyboard>", "<Be Right Back>", "<Do Not Disturb>");

str_replace($array1, $array2, $text);

--Output--
OK! <Be Right Back> then. Or <Away From Keyboard>, i maybe take longer time than i think. so please <Do Not Disturb> :)


so what i need help with is how i can store multiple strings in an array.
//Zeelia

RE: replace_str by roflmao456 on 05-06-2008 at 09:19 PM

code:
var text = "OK! brb then. Or afk, i maybe take longer time than i think. so please dnd :)";
var array1 = new Array("afk", "brb", "dnd");
var array2 = new Array("<Away From Keyboard>", "<Be Right Back>", "<Do Not Disturb>");

function str_replace(search, replace, subject){
    var f = search, r = replace, s = subject;
    var ra = is_array(r), sa = is_array(s), f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;

    while(j = 0, i--){
        while(s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f);
    }
     
    return sa ? s : s[0];
}

function is_array(m_var){
return (m_var instanceof Array);
}

str_replace(array1, array2, text);


read the JScript 5.6 documentation on arrays
or you can go to W3Schools :)
RE: replace_str by Zeelia on 05-06-2008 at 11:01 PM

Thank you very much :D
I will try it out since the code which i tried to run gave me errors


RE: replace_str by CookieRevised on 05-07-2008 at 02:09 AM

quote:
Originally posted by MeEtc
This should work well for you:
http://kevin.vanzonneveld.net/techblog/article/ja..._phps_str_replace/
Although it works, such loop code isn't needed though. JScript already has a (very powerfull) Replace function.

eg:
Debug.Trace("ThA mAn hit thA ball with the bat".replace(/A/g, "e"));
will output: "The men hit the ball with the bat"

As with everything, forum search
CookieRevised's reply to how do i replace this text? > see Part 3
CookieRevised's reply to [Question] Isn't string.replace supposed to replace all occurences ?
CookieRevised's reply to [I help them] VB2JS

PS: yes, you can also use arrays with some small coding (since the Replace function uses regular expressions and actually accepts a function reference as replacement text too). That is done several times already on the forums too (cba to search for it atm).

nevertheless nice code find
;)
RE: replace_str by Zeelia on 05-07-2008 at 11:02 PM

Ok alright, thank you now that you show me this example, i see how it works and i think that this way is much more easier and takes less space on the script (since the "php function" takes like 3 kb, which isn't that much anyway, on the script).
So thanks!