How to print word order backwards?

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
User avatar
tvs008
Forum Commoner
Posts: 29
Joined: Wed May 03, 2006 10:46 pm
Location: Seattle

How to print word order backwards?

Post by tvs008 »

Here's a string: "all cows eat grass"

How do we make this say "grass eat cows all"?

If I understand, this is not something strrev() does. So my thought was to put the string in a variable, put in into an array word by word by stripping spaces, then loop through the array backward (How do you loop through an array backward (x--) without going to -1, -2, etc?), then put the elements into a string form in a variable, then print it. That seem right?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

check out krsort()
User avatar
tvs008
Forum Commoner
Posts: 29
Joined: Wed May 03, 2006 10:46 pm
Location: Seattle

Post by tvs008 »

thanks,
im going to give that a try but i'd like to see it done manually as an excercise. i'll post results if i get anywhere

btw, what are the best burritos in phoenix?
i used to go to 'bertos alot ...miss that stuff
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

explode() will be useful here too
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

tvs008 wrote:btw, what are the best burritos in phoenix?
without question... Alfredos.

Carne Asada, enchilada style...mmmm
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Hay caramba! Carne Asada, con arroz, frijoles, salsa, crema, guacamole, grande como tu pierna. Que lo quiero comer!

I know this was like way off-topic, but, seriously, you were talking carne asada. :D
Post Reply