What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » replace_str

replace_str
Author: Message:
Zeelia
New Member
*


Posts: 11
– / Male / Flag
Joined: Dec 2007
O.P. replace_str
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?

This post was edited on 05-06-2008 at 06:37 PM by Zeelia.
05-06-2008 06:36 PM
Profile PM Find Quote Report
MeEtc
Patchou's look-alike
*****

Avatar
In the Shadow Gallery once again

Posts: 2200
Reputation: 60
38 / Male / Flag
Joined: Nov 2004
Status: Away
RE: replace_str
This should work well for you:
http://kevin.vanzonneveld.net/techblog/article/ja..._phps_str_replace/
[Image: signature/]     [Image: sharing.png]
I cannot hear you. There is a banana in my ear.
05-06-2008 08:24 PM
Profile PM Web Find Quote Report
Zeelia
New Member
*


Posts: 11
– / Male / Flag
Joined: Dec 2007
O.P. RE: replace_str
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

This post was edited on 05-06-2008 at 08:40 PM by Zeelia.
05-06-2008 08:36 PM
Profile PM Find Quote Report
roflmao456
Skinning Contest Winner
****

Avatar

Posts: 955
Reputation: 24
30 / Male / Flag
Joined: Nov 2006
Status: Away
RE: replace_str
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 :)

This post was edited on 05-06-2008 at 11:42 PM by roflmao456.
[quote]
Ultimatess6
: What a noob mod
05-06-2008 09:19 PM
Profile PM Web Find Quote Report
Zeelia
New Member
*


Posts: 11
– / Male / Flag
Joined: Dec 2007
O.P. RE: replace_str
Thank you very much :D
I will try it out since the code which i tried to run gave me errors
05-06-2008 11:01 PM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: replace_str
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
;)

This post was edited on 05-07-2008 at 03:36 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
05-07-2008 02:09 AM
Profile PM Find Quote Report
Zeelia
New Member
*


Posts: 11
– / Male / Flag
Joined: Dec 2007
O.P. RE: replace_str
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!
05-07-2008 11:02 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On