Page 1 of 1

count() and str_replace()

Posted: Tue May 25, 2004 7:44 am
by Dingbats
I'm making a l33tifier for my site, and this is a part of the code I use in the l33tifier function:

Code: Select all

$words = explode(" ", $str);
		for($i = 0; $i <= count($words); $i++)
		{
			$temp = rand(0, 1);
			if($temp == 0)	$word = "j00";
			if($temp == 1)	$word = "yuo";
			$words[$i] = str_replace("you", $word, $words[$i]);
		}
		$str = implode(" ", $words);
The string you put in the function call is called $str. As you can see, this is the code replacing "you" with either "yuo" or "j00". Some "you"'s changes to "yuo" and some to "j00".
But something is wrong in there. When I run open the l33tifier page it takes ages for the page to load, so I think that the for loop is infinite by some reason.
I can't find what's wrong in there. Maybe a foreach loop could solve the problem? I don't know how those work though. Do you know what's wrong?

Posted: Tue May 25, 2004 10:06 am
by launchcode
Your loop never ends - just take the = away from the for section (i.e. for ($i=0; $i < count($words); $i++) and it'll work fine.

Posted: Tue May 25, 2004 12:53 pm
by Dingbats
Thanks, it worked! :D