litlle help with implode and explode commands

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
avera
Forum Newbie
Posts: 3
Joined: Mon May 26, 2008 1:27 am

litlle help with implode and explode commands

Post by avera »

I have to make a program in php, where user enter a sentence and last word is showed as many times as there are words in it
example

I love sport
sport
sport
sport

Please help
RMcLeod1
Forum Newbie
Posts: 1
Joined: Thu Jun 05, 2008 4:20 am
Location: Somerset, England

Re: litlle help with implode and explode commands

Post by RMcLeod1 »

Code: Select all

<?php
$string = 'I love sport';
$words = explode(' ', $string);
for($i = 1, $num = count($words); $i <= $num; $i++) {
  echo $words[$num - 1] . '<br />';
}
?>
Last edited by RMcLeod1 on Thu Jun 05, 2008 4:30 am, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: litlle help with implode and explode commands

Post by Benjamin »

You'll want to use explode(), count(), str_replace(), and foreach() or for()
Scrumpy.Gums
Forum Commoner
Posts: 71
Joined: Thu Aug 30, 2007 2:57 pm
Location: Bristol, UK

Re: litlle help with implode and explode commands

Post by Scrumpy.Gums »

Assuming you've got as far as getting the user input, you can use something like:

Code: Select all

$words = explode(" ", $input);
echo str_repeat(end($words) . "\n", count($words));
 
Post Reply