Hi folks,

Working on a small project with PHP spell checking features (pspell) and wind up with strings containing both plain text (properly spelled words) and drop down lists (misspelled word suggestions) such as:

The large brown [dog] ate my [homework].

Where words in brackets are drop down lists containing the corrected spelling choices of the user, say from the original text string "The large brown dogg ate my homwork". Pretty typical pspell stuff.

My question: How can you parse a text string mixed with both plain text and drop down lists as above?

Obviously, I am looking for a final string of just text as in "The large brown dog ate my homework". No more drop down lists.

I attempted to capture the drop down list values via $_POST, but the problem is that the number of drop down lists, their names and their values will obviously change each time new text is entered and spelling errors are detected.

What I am looking for is a function capable of parsing the initial string, keeping plain text words and punctuation, but replacing the drop down lists with their selected values. Hopefully this all makes sense.

Have looked at many pspell examples and they all seem to go to the point of presenting the string with drop down lists inserted and then end there with no examples or instructions on rebuilding the final string for presentation back to the form/user.

I appreciate any and all advice and examples.

Thanks!!!

Jim

Member Avatar for Rhyan

I don't really understand your problem, but it seems you have the correct values you need already populated in the plaintext, however these values are surrounded by brackets, which cause the spell checker to throw errors, is that correct?

If this is your case, you can check this php functionality:
http://bg.php.net/manual/en/function.preg-replace.php
Otherwise, please explain more clearly...

Sorry for the confusion. The brackets in my example indicate where drop down lists might appear in the string. There are no actual brackets in any of the strings, unless they are input by the user within their submitted text.

The text string will change each time a user submits some text into a scrolling textarea and clicks a "Check Spelling" submit button. This is spell checker function. Their text is then passed to another PHP script for spell checking by pspell.

Their text is returned back to them within a scrolling div, with drop down lists inserted into their original text string in place of misspelled words, allowing them to choose a suggested alternative word from the pspell dictionary. Once they have made all changes from the drop down lists, they resubmit the text via a "Done" submit button and the string my script receives back would look like my example above. The string might actually be any length up to 750 chars (my allowed maximum) and could contain any number of drop down lists (dependant on the number of misspelled words in their original text). The drop down lists can appear in different locations each time as it is dependant on where the misspelled words were in their text and of course, what their submitted text actually looks like.

Once I have the corrected string (with drop down lists in place), I need to parse the entire string to determine two things along the way: 1. Is each word encountered plain text or a drop down list. If plain text, simply grab it and insert it into a new string. If a drop down list, grab its selected value and insert it into the new string. Once the entire string is parsed, I would wind up with a text-only string (drop down lists removed) to pass back to the user for final approval. If they are satisifed with the changes, it is inserted into a database field. If not, the process starts over again.

In any case, I worked on it until late last night and came up with two workable options, one with the help of a forum member from another site. 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.

I appreciate your time and willingness to help me, but have found solutions since my post here. I meant to post this last night before quitting for the night, but it got very late.

Thanks,

Jim

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.