Page 1 of 1

litlle help with implode and explode commands

Posted: Thu Jun 05, 2008 4:13 am
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

Re: litlle help with implode and explode commands

Posted: Thu Jun 05, 2008 4:29 am
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 />';
}
?>

Re: litlle help with implode and explode commands

Posted: Thu Jun 05, 2008 4:30 am
by Benjamin
You'll want to use explode(), count(), str_replace(), and foreach() or for()

Re: litlle help with implode and explode commands

Posted: Thu Jun 05, 2008 4:32 am
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));