Page 1 of 1

Need help with writing a php code or data form

Posted: Thu Nov 06, 2014 10:16 am
by Blackdevil
Hi folks,
I need urgent help with code here.I'm asked to create a data form that accepts odd number of words in a given sentence
Say for example,I enter "Hello my name is Jason Smith Flaunders"
The code output should be :Flaunders Smith Jason is name my Hello
It should also display the number of words.
Output:Smith Jason is name my Hello
words=7.
Can anyone help me to create the data form that outputs this? Please help me guys.Its for my univ purpose.
Thanks.

Re: Need help with writing a php code or data form

Posted: Thu Nov 06, 2014 10:36 am
by Celauran
What have you tried so far? Where are you getting stuck?

Re: Need help with writing a php code or data form

Posted: Thu Nov 06, 2014 12:32 pm
by Blackdevil
I have tried using explode and then rearrange but the explode function is not properly working.So i was hoping for a solution.

Re: Need help with writing a php code or data form

Posted: Thu Nov 06, 2014 12:34 pm
by Celauran
How is explode not working? Care to post some code?

Re: Need help with writing a php code or data form

Posted: Thu Nov 06, 2014 2:03 pm
by Blackdevil
Ok.Explode is working.I had some errors so it wasnt working earlier..
I prepared a form that accepts the user data as a string.
code:

Code: Select all

<form action="welcome_get.php" method="get">
String: <input type="text" name="string"><br>
<input type="submit">

</form>
And i have the code that performs the reversal of a string
code:

Code: Select all

$str = "Hello world. It's a beautiful day.";
echo "<br />\n";
echo "<br />\n";
echo"The no of words is";
echo "<br />\n";
echo str_word_count($str);
echo "<br />\n";
echo "<br />\n";
print_r (explode(" ",$str));

$a=array("$str");
$reverse=array_reverse($a);
$preserve=array_reverse($a,true);
echo "<br />\n";
echo "<br />\n";
echo implode(' ', array_reverse(explode(' ', $str)));
I want the string entered by the user in the form to be processed and reversed so the whichever string entered will outcome with its words reversed but not the letters.So how can i do this?

Re: Need help with writing a php code or data form

Posted: Thu Nov 06, 2014 4:28 pm
by Celauran
The string you'll want to operate on, based on the structure of your form, is $_GET['string']