Jump to content

geomc

Newly Registered
  • Posts

    7
  • Joined

  • Last visited

Posts posted by geomc

  1. How do you selectively BOLD output array elements from three arrays?

    My code has only one output line. I am creating a madlib php program

    and I want my nouns to be in bold, my verbs to be italicized, and my

    adjectives to be underlined. Where would I implement this? Thanks.

     

    Here is my code:

     

    $counter=0;
    $nounCounter=0;
    $verbCounter=0;
    $adjCounter=0;
    $newSentArray = $sentArray; // copy $sentArray
    while ( $counter < count($sentArray)) // loop over words in array
    {
         if($sentArray[$counter] == "NOUN" or $sentArray[$counter] == "NOUN." or $sentArray[$counter] == "NOUN!")
         {
             // replace current word with noun
             $newSentArray[$counter] = $nounArray[$nounCounter];
             $nounCounter++; // increment noun, so next noun in used for replacement
         }
            

        elseif($sentArray[$counter] == "VERB" or $sentArray[$counter] == "VERB." or $sentArray[$counter] == "VERB!")
        {
             // replace current word with verb
             $newSentArray[$counter] = $verbArray[$verbCounter];
             $verbCounter++; // increment verb, so next verb in used for replacement
        }

        elseif($sentArray[$counter] == "ADJECTIVE" or $sentArray[$counter] == "ADJECTIVE." or $sentArray[$counter] ==  

       "ADJECTIVE!")
        {
             // replace current word with adjective
             $newSentArray[$counter] = $adjArray[$adjCounter];
             $adjCounter++; // increment adjective, so next adjective in used for replacement
        }
            
         $counter++;
    }

    // implode words array into a string
    echo implode(' ', $newSentArray);

  2. The code works perfect for me. How are filling in the form in project1.html?

     

    I am setting number of nouns to 2

    Typing 'cat dog' (without quotes) to the nouns field

    and typing 'I bought a NOUN and NOUN' (without quotes) into the initial sentence field

     

    My complete output of project1.php is

    I was typing the sentence in with a period at the end like this: I bought a NOUN and a NOUN.

    I took out the period and now it works...Thank you so much for your help.

  3.  

    How about a preg_replace and a callback that holds the array of replacements:

     
    echo preg_replace_callback('~NOUN~', function ($matches) {
        static $intCounter = 0;
        $arrReplacements = array('cat', 'dog');
        return strtolower($arrReplacements[$intCounter++]);
    }, 'Today I bought a NOUN and a NOUN');
    

    I appreciate the help, but the php code I am working on has the user enter "any" number of nouns, and then the user has to type a sentence like "I bought a NOUN and a NOUN." The php code is supposed to replace the first NOUN with anything from the noun list, then it is supposed to replace the second NOUN with anything from the noun list. So, in essence, you would get "I bought a cat and a dog" if you entered (cat dog) into the submit form.

  4.  

    Code to get requinix's suggestion to work

    $counter=0;
    $nounCounter=0;
    $newSent = $initialSent;
    while ( $counter < count($sentArray)) // loop through words array
    {
         if($sentArray[$counter] == "NOUN")
         {
             $pos = strpos($newSent, $sentArray[$counter]); // get position of noun placeholder in the string
             $newSent = substr_replace($newSent, $nounArray[$nounCounter], $pos, strlen($sentArray[$counter])); // replace the noun with the current noun in $nounArray
             $nounCounter++;  // increment $nounCounter, so next noun is used for replacement
         }
            
         $counter++;
    }
    echo $newSent;
    

    Or an alternative would be to replace in the word in the array at current position.

    $counter=0;
    $nounCounter=0;
    $newSentArray = $sentArray; // copy $sentArray
    while ( $counter < count($sentArray)) // loop over words in array
    {
         if($sentArray[$counter] == "NOUN")
         {
             // replace current word with noun
             $newSentArray[$counter] = $nounArray[$nounCounter];
             $nounCounter++; // increment noun, so next noun in used for replacement
         }
            
         $counter++;
    }
    // implode words array into a string
    echo implode(' ', $newSentArray);

     

     

    OK, you are getting really close! I tried the two codes above and they both are giving me this now: "I bought a cat and a NOUN."  The only problem I have

    now is getting the second NOUN to be replaced with dog. What could I fix in the code you gave me to do this? Thanks!

  5. Hi, I appreciate the help. I did try this code,but now I'm getting "I bought a dog and a dog." I am sorry I am such novice on this and the problem I am having is what you described, which is having it continue from where the previous replacement happened. It's not working. Here is how I test my program, on the submit form, I select 2 nouns, I enter the nouns cat and dog where I enter the nouns. Then at the bottom I type in the initial sentence of "I bought a NOUN and a NOUN." Then I click submit. What I want the PHP code to do is replace each instance of NOUN with a different noun from the list(cat or dog). I want the final sentence to look like this: "I bought a cat and a dog." Any other help would greatly be appreciated. Thanks.

     

    Here is my code again if you need to see it:

     

    $counter=0;

    $nounCounter=0;

    if($sentArray[$counter]=="NOUN")

    {

    $pos = strpos($initialSent, $sentArray[$counter]);

    $newSent = substr_replace($initialSent, $nounArray[$nounCounter], $pos, strlen($sentArray[$counter]));

    echo $newSent . '<br .>';

  6. str_replace() will replace every instance of the string, not just one.

     

    You need a narrower setup like with strpos and substr_replace.

    $pos = strpos($initialSent, $sentArray[$counter]);
    $newSent = substr_replace($initialSent, $nounArray[$nounCounter], $pos, strlen($sentArray[$counter]));
    (untested but looks right)

     

    After this you'll have to modify the code so that it doesn't keep trying to replace in the initial string ($initialSent) every time but can continue from where the previous replacement happened ($newSent).

     

     

    Hi, I appreciate the help. I did try this code,but now I'm getting "I bought a dog and a dog." I am sorry I am such novice on this and the problem I am having is what you described, which is having it continue from where the previous replacement happened. It's not working. Here is how I test my program, on the submit form, I select 2 nouns, I enter the nouns cat and dog where I enter the nouns. Then at the bottom I type in the initial sentence of "I bought a NOUN and a NOUN." Then I click submit. What I want the PHP code to do is replace each instance of NOUN with a different noun from the list(cat or dog). I want the final sentence to look like this: "I bought a cat and a dog." Any other help would greatly be appreciated. Thanks. Oh, and one more thing I forgot to mention, my initial sentence is exploded (sentArray) into an array and the nouns the user enters are exploded into an array(nounArray). Thanks.

  7. Can anyone help me? I am a student and I have been working on a PHP project for over two weeks with no luck on one piece of code. The project is a small "Madlib" project, but I'm only having trouble with one piece of code. Any takers? All I'm trying to do is get a sentence like "I bought a NOUN and NOUN." to be replaced with "I bought a cat and a dog." The problem is, I get this: "I bought a cat and a cat." I have tried all the loops I can try. Yes, I'm a newbie, but I'm desperate and can't figure this out. I have my html and php code, also the URL to the website. Any help would greatly be appreciated. Thanks.
     

    I have attached my html and my php file to this posting.

     

    project1.htmlproject1.php

     

     

    I have posted the small piece of code I am having a problem with down below:

     

    $counter=0;
    $nounCounter=0;
    while ( $counter < $sentLength and $nounCounter < $nounLength)
    {
         if($sentArray[$counter]=="NOUN")
         {
         $newSent = str_replace($sentArray[$counter], $nounArray[$nounCounter], $initialSent);
         }
         $counter++;
         
         echo $newSent . '<br .>';

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.