Need help parsing strings w/ plain text and drop down lists

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
jcassan
Forum Newbie
Posts: 6
Joined: Sun Nov 09, 2008 4:57 pm

Need help parsing strings w/ plain text and drop down lists

Post by jcassan »

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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help parsing strings w/ plain text and drop down lists

Post by requinix »

jcassan wrote:My question: How can you parse a text string mixed with both plain text and drop down lists as above?
What does this string look like?
jcassan
Forum Newbie
Posts: 6
Joined: Sun Nov 09, 2008 4:57 pm

Re: Need help parsing strings w/ plain text and drop down lists

Post by jcassan »

As an example from HTML it looks like:

Code: Select all

The <select name="position[4]">
  <option value="large" selected>large</option>
  <option value="largo">largo</option>
  <option value="lag">lag</option>
  <option value="Lark">Lark</option>
  <option value="lark">lark</option>
  <option value="Lang">Lang</option>
  <option value="Lara">Lara</option>
  <option value="Lari">Lari</option>
  <option value="Lars">Lars</option>
  <option value="lard">lard</option>
</select> brown <select name="position[15]">
  <option value="cow" selected>cow</option>
  <option value="cowl">cowl</option>
  <option value="cows">cows</option>
  <option value="cower">cower</option>
  <option value="wow">wow</option>
  <option value="CO">CO</option>
  <option value="Co">Co</option>
  <option value="co">co</option>
  <option value="cw">cw</option>
  <option value="Crow">Crow</option>
  <option value="chow">chow</option>
  <option value="crow">crow</option>
  <option value="vow">vow</option>
  <option value="cowed">cowed</option>
</select> ate my <select name="position[27]">
  <option value="homework" selected>homework</option>
  <option value="homeworks">homeworks</option>
  <option value="homeworker">homeworker</option>
  <option value="homework's">homework's</option>
</select>
 
And in a browser:

The [drop down list] brown [drop down list] ate my [drop down list].

Note: From a PHP string explode, it prints and/or echoes as above as well. Contains the original string with drop down lists in place.
jcassan
Forum Newbie
Posts: 6
Joined: Sun Nov 09, 2008 4:57 pm

Re: Need help parsing strings w/ plain text and drop down lists

Post by jcassan »

As further info:

Here are the steps in order:

1. User is presented with a form page with a scrolling textarea
2. User enters text and clicks a "Check Spelling" submit button
3. Text is analyzed by another PHP script using pspell
4. A modified string is returned as ($_SESSION['string']), which includes all original text (where correctly spelled) and drop down lists in place of misspelled words.
5. The new string needs to be parsed to return all text, with the drop down selected values in place of the drop down lists themselves. (this is the step I can't figure out)

Obviously PHP recognizes each string component as doing the following displays the string:

Code: Select all

$strout = explode(" ",$_SESSION['string']);
foreach($strout as $name=>$val) {
echo $val.' ';
 }
Produces a correctly displayed string such as "The [large] brown [cow] ate my [homework]."

The PHP explode function recognizes each drop down list as such and prints it as any other "exploded" segment. What I need is a function that basically looks at each segment and identifies: If "plain text or punctuation" place into new string "as is" and If "drop down list" place selected value into same new string. Once it has looped through all segments, the new new string will be only text, with all drop down lists removed such as "The large brown cow ate my homework." That string will be returned to the user in the original form textarea as corrected text for approval.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help parsing strings w/ plain text and drop down lists

Post by requinix »

Code: Select all

echo preg_replace('#<select[^>]+>.*?<option value="([^"]+)" selected.*?</select>#is', '$1', $_SESSION["string"]);
Are you sure there isn't an option to pspell to automatically do this?
jcassan
Forum Newbie
Posts: 6
Joined: Sun Nov 09, 2008 4:57 pm

Re: Need help parsing strings w/ plain text and drop down lists

Post by jcassan »

Checked all pspell doc I could find and don't see anything specifically geared towards rebuilding the strings once the selections are made from the drop down lists. Also looked at as many pspell example scripts as I could find on the net.

I tried your preg_replace example and I still get the actual session string with all drop down lists intact. Looked promising though. I certainly appreciate the effort. It is maddening...
jcassan
Forum Newbie
Posts: 6
Joined: Sun Nov 09, 2008 4:57 pm

Re: Need help parsing strings w/ plain text and drop down lists

Post by jcassan »

When running your line against $val, it does remove the drop down lists, but lists out all of the list options instead of just the selected value. Much closer than I ever got...

Here is the output:

ThelargelargolagLarklarkLangLaraLariLarslardbrowncowcowlcowscowerwowCOCococwCrowchowcrowvowcowedatemyhomeworkhomeworkshomeworkerhomework's.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help parsing strings w/ plain text and drop down lists

Post by requinix »

Code: Select all

<?php
 
$text = 'The <select name="position[4]">
  <option value="large" selected>large</option>
  <option value="largo">largo</option>
  <option value="lag">lag</option>
  <option value="Lark">Lark</option>
  <option value="lark">lark</option>
  <option value="Lang">Lang</option>
  <option value="Lara">Lara</option>
  <option value="Lari">Lari</option>
  <option value="Lars">Lars</option>
  <option value="lard">lard</option>
</select> brown <select name="position[15]">
  <option value="cow" selected>cow</option>
  <option value="cowl">cowl</option>
  <option value="cows">cows</option>
  <option value="cower">cower</option>
  <option value="wow">wow</option>
  <option value="CO">CO</option>
  <option value="Co">Co</option>
  <option value="co">co</option>
  <option value="cw">cw</option>
  <option value="Crow">Crow</option>
  <option value="chow">chow</option>
  <option value="crow">crow</option>
  <option value="vow">vow</option>
  <option value="cowed">cowed</option>
</select> ate my <select name="position[27]">
  <option value="homework" selected>homework</option>
  <option value="homeworks">homeworks</option>
  <option value="homeworker">homeworker</option>
  <option value="homework\'s">homework\'s</option>
</select>';
 
echo preg_replace('#<select[^>]+>.*?<option value="([^"]+)" selected.*?</select>#is', '$1', $text);
 
?>
On line 33 you need to add slashes to escape the apostrophes. Seems the forum software doesn't like it.

Are you sure the string is exactly as you portrayed?
jcassan
Forum Newbie
Posts: 6
Joined: Sun Nov 09, 2008 4:57 pm

Re: Need help parsing strings w/ plain text and drop down lists

Post by jcassan »

String was correct, but so are you... Needed to escape any instances of the apostrophe to prevent PHP from thinking it hit an end prematurely.

Put simply, it does the trick. You are a true genius.

Thank you so much. I won't be up all night (again) trying to figure it out.
Post Reply