String.replace() method for ActionScript2
Posted on 23rd November 2007 in ActionScript | 21 Comments »
Another snippet for free — String.replace() prototype for Flash that can be used to replace a pattern in a String.
The code of the String.replace() prototype is very simple and somewhat similar to the PHP’s str_replace function:
Example:
// initial string with a placeholder var str:String = '$person is welcome to flash tekkie'; // replace $person with 'Flash developer' and trace it var replacedStr:String = str.replace('$person','Flash developer'); trace(replacedStr); |
As seen above method takes 2 arguments. Firstly a string pattern to search for and secondly a string to replace the pattern(s) found. Method returns a String.


21 Responses
Thanks for the script. It was useful for me
Thank you very much for the script.I was looking for something like that
or even simpler:
return this.split(searchStr).join(replaceStr);
Yes, why not.
thanks!
thank you !
it display “undefined”
Yehia: as you can see, quite a few people have found it working and I can assure you it is. Check your implementation, it’s a trivial and straightforward approach.
thanks , But How do I use more than one word to replace it
String.prototype.replace = function(search, replacer):String{
return this.split(search).join(replacer);
}
The script is for AS 3.0, not 2.0 :/
This is why @Yehia gets “undefined”
Plamen: This snippet was used in the AS2 Flash CMS framework and is certainly AS2. In AS3 all this is a bit simpler.
very good for this code
String.prototype.replace = function(search, replacer):String{
return this.split(search).join(replacer);
}
.
THANK YOU VERY MUCH IT HELPED ME ALOT!
Thank you, it was very helpfull!
can give us exemple? where to put words to change and where to put words which will be changed .
can you give exemple with those words pls
look for this word ( people )
and replace it with (planet)
pls im interested about this code but didnt find how to use it.
thanks
i used the code but i get this error
Parse error: syntax error, unexpected ‘=’ in H:\installed here\EasyPHP5.2.10\www\essaylocal\new.php on line 2
should i create a as file ? if so , how to call it via php !! pls more explication
The code in this post is ActionScript 2, a dialect of ECMAScript similar to JavaScript. It will not work in PHP.
To change words within the text in PHP, you should use str_replace() function.
For example:
For a more complex solutions check preg_replace() although you should be aware that
preg_replace()should be avoided whenever possible due to the performance.str_replace()is much faster.works!!! thanks!!
thanks a bunch.