0

I am lost with a problem I am working on.

I have a HTML form with a few radio buttons and a submit button. From this form, the user will be able to select one of the radio buttons, and then click submit. This will start the PHP script and the script should pull out words from a string that do not match the number that was selected from the radio button.

Whereas I am comfortable with HTML, PHP is giving me a little more trouble.

Here is where I am stuck:

    $myTok = strtok($fileContents, "\"!@#$%^&*(1234567890).,:;'_?\n\t-[]~{}");

while ($myTok != false){

    $myTok = strtok( "\"!@#$%^&*(1234567890).,:;'_?\n\t-[]~{}");

        foreach ($myTok as $key => $value){
            if (strlen($value == 1)){
                                   //not sure how to return the value to the $resultWords variable here

}
}

$resultWords = 

I have a string (ex. "I am having a hard time with the PHP script.") I am using the strtok() function remove any delimiters that are not needed and that would count as an extra character to the $value in my foreach loop but I am wanting ONLY the words.

So with the foreach loop, I am going through every token value to see if the word matches the length that the user chose. These are the words that I want to remove from the string. So, my new string would look like "I am having a the PHP script" if the user chose radio button 4. I want to do this using a foreach() loop. My biggest confusion is how to return the modified string from the loop back out to the variable $resultWords or if I even have the beginning of my foreach loop setup correctly.

Would it be recommended to use the explode() function to make it all an array before the foreach loop? Sorry, I am a bit newbish with this.

5
  • You can't "return" a value in that context. What you have to understand is variable scope. That being said, have you considered using str_replace() instead? Commented Oct 26, 2012 at 18:08
  • Maybe you just want to assign it as array $resultWords[] = $value; for later use? Note that you could collapse all that code into preg_match_all('/\w+/', $input, $words); Commented Oct 26, 2012 at 18:11
  • I appreciate the way you have formatted your question. Well explained and descriptive. I appreciate your effort as well. Commented Oct 26, 2012 at 18:13
  • Thanks for the suggestions! I'll take a look at using it as an array instead. And @NullUserException I looked at str_replace() but I couldn't seem to figure it out for my use. Like I said, I am rather new, and am trying to learn on my spare time. There's SO much out there! :) Commented Oct 26, 2012 at 18:20
  • I was writing this comment and don't feel have it wasted: ) Some hints on debugging: 1. turn all possible error reporting on. 2. Get rid of all the @s in your code and NEVER ever use them again. 3. var_dump() as much values as possible. Variables, function returns, intermediate results - and compare them with expected ones. Commented Aug 14, 2013 at 14:02

1 Answer 1

2

You can modify your script in the below way.

$resultWords  = array();
foreach ($myTok as $key => $value){

 if (strlen($value == 1)){

        $resultWords[] = $value;                   

    }
}

//Make use of the array now. 
foreach($resultWords as $key => $value) {

    //$value will contain the words

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.