count() and str_replace()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dingbats
Forum Commoner
Posts: 25
Joined: Fri Dec 05, 2003 10:53 am

count() and str_replace()

Post 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?
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
Dingbats
Forum Commoner
Posts: 25
Joined: Fri Dec 05, 2003 10:53 am

Post by Dingbats »

Thanks, it worked! :D
Post Reply