Need help with writing a php code or data form

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
Blackdevil
Forum Newbie
Posts: 3
Joined: Thu Nov 06, 2014 9:56 am

Need help with writing a php code or data form

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

What have you tried so far? Where are you getting stuck?
Blackdevil
Forum Newbie
Posts: 3
Joined: Thu Nov 06, 2014 9:56 am

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

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

How is explode not working? Care to post some code?
Blackdevil
Forum Newbie
Posts: 3
Joined: Thu Nov 06, 2014 9:56 am

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

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

The string you'll want to operate on, based on the structure of your form, is $_GET['string']
Post Reply