Jump to content

Please help! PHP Problem - I've spent 7 hours a day for 7 days trying to figure this out.


Go to solution Solved by geomc,

Recommended Posts

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 .>';

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).

Edited by requinix

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.

Edited by geomc

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 .>';

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);
Edited by Ch0cu3r
  • Solution

 

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!

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');

 

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.

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!

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

 

 

Number of Nouns: 2

Number of Verbs: 0

Number of Adjectives: 0

List of Nouns: cat dog

List of Verbs: 

List of Adjectives: 

Initial Sentence: I bought a NOUN and NOUN

 

FINAL OUTPUT: 

Nouns: cat, dog

Verbs: 

Adjectives: 

INITIAL SENTENCE: 

I bought a NOUN and NOUN

 

YOUR MAD LIB SENTENCE!: I bought a cat and dog

 

Sentence Length 24

Noun Length 7

Array ( [0] => I [1] => bought [2] => a [3] => NOUN [4] => and [5] => NOUN ) Sentence Array1

Array ( [0] => cat [1] => dog ) Noun Array1

Array ( [0] => ) Verb Array1

Array ( [0] => ) Adjective Array1

 

TEST SECTION

Noun Sentence Count: 2

Verb Sentence Count: 0

Adjective Sentence Count: 0

Noun Count Result: 2

Verb Count Result: 0

Adjective Count Result: 0

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.