423,850 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

Parsing text and drop down list values from a string

P: 3
Hello folks. I am new to these forums and have something, which has been stumping me for little while.

I am using pspell to spellcheck a scrolling textbox (textarea) containing user input.

I analyze each word and rebuild the text string as a combination of correct text (not misspelled) and a drop down list offering suggestions in place of any misspelled words. Pretty common stuff using pspell.

I display the new string so that the user can select a replacement word from the drop down lists. Each drop down list is programatically named as created as <select name="position[$pos]"> with $pos being its actual position in the original text string. Makes it easier to keep track of where the list belongs. All of that works fine.

On to the question... Once the user has selected replacement words and clicks the "Done" submit button, I call the same PHP script again and now have a string, which for example looks like:

How many times did the [big] brown [dog] jump over the [fence]?

Where big, dog and fence are actually the drop down lists with the selections made to correct "bigg", "dogg" and "fense" misspellings respectively.

The question: Using PHP, How can I parse the complete string, resulting in one string with the drop down lists removed and only the text remaining?

i.e. the string would be "How many times did the big brown dog jump over the fence?"

Handling the regular text has presented no problems, but I have been unable to detect and extract the drop down values in between. The obvious problem stems from the drop down lists using html tags to define them.

Any suggestions and/or examples will be greatly appreciated.
Nov 8 '08 #1
Share this Question
Share on Google+
3 Replies


dlite922
Expert 100+
P: 1,584
From what I see, you need to grab the values of the select box.

This seems too easy for someone who knows this much. So, why can't you just grab the select values during a post (form submission) ?

If you want the drop downs to have the same name, name them the same except add brackets [] to the end of the name. then you'll find your $_POST['name'] is an array of the values.

Hope that helps,




Dan
Nov 9 '08 #2

P: 3
Considered the option of snagging either $_GET or $_POST variable assignments, but I am still a bit confused as to how to do it, since the variables (and their names) will change dynamically as the input text does. In other words, the number of drop down lists will match the number of misspelled words. So, if there are 10 misspelled words in the the input text, there will be 10 dropdown lists.

The problem I am facing is that unlike typical form submissions where you know the name of any given field (making it easy to snag its value following a post) such as $_GET['myField'], the dynamilcally generated drop down lists will be named as position[x], where x is the misspelled word's position in the original text. For instance, one would be named position[5] if its original misspelled word was found at character position 5.

I am still a little confused as to the best way to track the dynamically created values. If I have several drop down lists created dynamically and their names are position[2], position [5], position [7], etc. they don't follow a numerical order but rather indicate the text positions they should be reinserted to. The final text string needs to be reassembled as [words that aren't misspelled] + [appropriate drop down selection, where a word was misspelled] + [more words that aren't misspelled] + [appropriate drop down selection, where a word was misspelled] + [more words that aren't misspelled], etc. Thought about storing the values in an array, but that won't work either as the process can't access them in the array by either record number [0], [1], [2], etc or by position number as those numbers won't be known values.

Since I don't know how many drop down lists may be created or their exact names, I can't wrap my brain around how to get the selected values AND insert them back into the string in the correct position following the form submission.

I assumed using the "position[x]" naming convention was a good idea as it would tell me where to insert its value back into the string. i.e. If there are 2 correctly spelled words such as "The brown " they end at character count 10. So my corrected drop down selection of "dog" would be named "position[11]", meaning that it would be inserted following the space (character 10) after the n (character 9) in brown.

Seemed like a good idea, but I am still struggling with how to reassemble (in the correct order) the two dynamically changing variable types, 1. Plain text (correctly spelled words) and 2. Drop down list selections for misspelled words.

This is why I was hoping for a way to identify a drop down list within the string and could grab its value (at that point) to merge it into the final string. i.e. Each word would be checked and added to the new string until a drop down list was encountered, then its selected value would be added in and so on, until all words and selected values were merged back into the new string. Then the final string would be passed back to the user in a text box to approve and submit.

As my final rambling, I have tried an explode on the string using a space as the delimiter and it successfully breaks the components into individual word pieces as expected. When I echo or print each separate piece in order, they do display correctly, including the drop down lists and their selected values. This is what led me to believe that they can be identified as drop down lists versus a plain text word. If that is true, I should be able to grab their value when encountered and pass it to the final string.

Hopefully I haven't made this more confusing and will gladly accept any suggestions. I do get the point about getting the values from a post, but am still unclear as to how to know what to query $_GET or $_POST for as the names will be unknown...

Thanks for the advice.
Nov 9 '08 #3

P: 3
Solved this issue...

I worked on it until late last night and came up with two workable options. The first, uses a preg_replace function to identify <select> and </select> options within the text, indicating where a drop down list resides. It then replaces the drop list with its own selected value. Works very well for parsing out the drop down lists in the string. The second solution involves naming each drop down list as position[x], where x is the word position within the string where the misspelled word was found. I can then access the $_POST array for "position" and replace the drop down lists with the appropriate posted value when its position within the string has been reached. Both solutions work well. Still deciding which is best for my application.

Thanks for the advice, it got me going in the right direction.
Nov 10 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.