I would like to create script where user enters two sentences. Sentence is terminated by dot (.) character. Provide a submit button "Reverse words in sentence". On submit button click, send the input sentence as Ajax request using dynamically created iframe. The backend should reverse the words in each sentence and send back the result to be displayed.
For Example, if the two sentences are:
"I am a good student. I will get good job."
The displayed results should look like
"student good a am I. job good get will I."
For PHP you have a neat little function called explode() which will create an array based on a string and a separator.
So given your example:
Code:
$sentences_array = explode('.','I am a good student. I will get good job.');
foreach ($sentences_array as $sentence){
$words_array = explode(' ', $sentence); //create array from string (separator ' ')
$words_array = array_reverse($words_array); //invert order of items
$sentence = implode(' ',$words_array); //glue array together with ' '
}
I've not tested this but it should show you the right direction..
Laurent
Last edited by blaurent; August 28th, 2011 at 04:41 PM.
I assume all you want it to do is reverse the words of the sentences right?
So why not just go with a html form
Code:
<html>
<body>
<form id="reverse_words_form" action="post" method="">
<textarea id="input_sentences"></textarea>
<label for="input_sentences">Please enter some sentences..</label>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Then I would use jQuery to add a click-handler to the submit button which would sent the form data using post (as an ajax request) to your back-end php script.
If you've never done AJAX before you should probably read up on it.
I used this not too long ago so here's the function reference. There are some examples on this page so don't be afraid of playing around with it. In your case I don't think you'd need to use JSON or XML as a return. I guess a simple string return would work.
Bookmarks