Hello.

I'm having a slight bit of trouble and I thought this would be a great place to look for help. Here's the code:

$words = file("$path/words.txt");

foreach ($words as $phrase){

if (strpos($phrase, "shop")){
$category = "shop";

}elseif(strpos($phrase, "dvd")){
$category = "dvd";

}elseif(strpos($phrase, "game")){
$category = "game";
}

}else{
$category = "main";
}

echo "$phrase $catgeory";

}

Basically, I'm trying to look through a bunch of phrases and categorising these phrases, according to the text they contain.

It all works fine without the foreach loop, but once I add that it always says $category = "main".

I would greatly appreciate any help you can give me! 🙂

Regards

Matt

    try

    foreach ($words as $key => $phrase){ 
    

    instead of

    foreach ($words as $phrase){ 
    

      Thanks for the reply, but unfortunately that does exactly the same thing 🙁

      Any other ideas?

      Thanks

      Matt

        I don't think that strpos() is the right function to use. I assume that the file containing the words contains one word per line, and you want to compare. Why can't you just use

        foreach ($words as $phrase){ 
        
        if ($phrase ==  "shop"){ 
          $category = "shop";
        }
        ...etc...
        

        If $phrase could possibly be 'shops' or 'shopping', then you should use substr(), not strpos().

        hth

          Unfortunately, there are multiple words per line, which is why I was using strpos 🙁 eg. "cd shops" "shopping in chicago"

          Thanks very much for the response though, and if you've got any other thoughts, I'd love to hear them.

          Regards

          Matt

            Then you probably need to use regular expression or limit what can appear in the text file. substr, strpos and any other direct comparison function is going to fall over, consider:

            $phrase = "shop";
            if(strpos($phrase, "shopping")){
              $category = "shop"; // This one is OK
            }
            
            $phrase = "hop";
            if(strpos($phrase, "shop")){
              $category = "shop"; // This one is wrong
            }
            

              aah... now i understand what you are trying to do - i also think that strpos is the wrong function...

              i would make sure to only have one word per line and use explode:

              path/words.txt:

              shopping chicago
              cd shops
              
              $words = file("$path/words.txt"); 
              
              foreach ($words as $phrase){ 
              
              $split = explode (" ", $words);
              
              echo "$split[0] $split[1]<BR>"; 
              
              }
              

                Note that if the word is at the beginning of the line then strpos returns 0 which evaluates to false.

                Use
                if (strpos($phrase,'shopping') !== false)

                  Thanks a lot you guys, I really appreciate the help (great tip ahundiak)! It's still not working though, so I'll be a bit more specific with exactly what I'm trying to do:

                  Text file(which will actually contain quite a large number of phrases):
                  
                  shopping in new york
                  dvd rentals
                  live rock music in la
                  cd shops
                  harry potter dvds
                  latest pc games
                  
                  $words = file("$path/words.txt"); 
                  
                  foreach ($words as $phrase){ 
                  
                  if ((strpos($phrase, "shop")!== false) OR ((strpos($phrase, "store")!== false)){ 
                  $category = "shop"; 
                  
                  }elseif(strpos($phrase, "dvd")!== false){ 
                  $category = "dvd";
                  
                  }elseif ((strpos($phrase, "cd")!== false) OR ((strpos($phrase, "music")!== false)){ 
                  $category = "audio"; 
                  
                  }elseif(strpos($phrase, "game")!== false){ 
                  $category = "game"; 
                  } 
                  
                  }else{ 
                  $category = "main"; 
                  } 
                  
                  echo "$phrase $catgeory"; 
                  }
                  

                  All the (strpos($phrase, "$key")!==false) functions work perfectly if $phrase is defined as one variable eg.

                  $phrase = "shopping in new york";
                  
                  if ((strpos($phrase, "shop")!== false) OR ((strpos($phrase, "store")!== false)){ 
                  $category = "shop"; 
                  
                  }elseif(strpos($phrase, "dvd")!== false){ 
                  $category = "dvd";
                  
                  }elseif ((strpos($phrase, "cd")!== false) OR ((strpos($phrase, "music")!== false)){ 
                  $category = "audio"; 
                  
                  }elseif(strpos($phrase, "game")!== false){ 
                  $category = "game"; 
                  } 
                  
                  }else{ 
                  $category = "main"; 
                  } 
                  
                  echo "$phrase $category";
                  

                  It's once I add the foreach loop to make this process repeat that the issues begin... I know very little about arrays really, so this possibly where the problems begin.

                  Thanks again for all your help, and if you got any suggestions, please let me know.

                  Regards

                  Matt

                    Write a Reply...