Using str_word_count to count letters within the opponent word












0















user 1 writes a word (word1) and I want to count how many letters of the word1 there are within the opponent word (word2).
At first I used the strpos function but it doesn't seem to count reppeated letters so I change to the str_word_count function but no matter what the word1 is the countex is always 5
This is my code:



 for($i=0;$i<10 && ($rowword=mysqli_fetch_assoc($result)) ;$i++){

$aword1 = str_split($rowword['word']);
$word2=$row['word2']; //$row comes from another query that gets the word of the opponent
$countex=0;

for($i=0;$i<5;$i++){
$currentletter=$aword1[$i];

if(str_word_count($word2,0,$currentletter)){
$countex++;
}
}

} //end of outter for


Can someone tell me what is the problem?



Example of what I want to achieve:



word2=annoy, word1=again
in this example countex should be 2 because in the word "again" the letter "a" and "n" exist in the word "annoy"










share|improve this question

























  • What a result do you expect if any letter is present some times in a word? May be, you show some examples?

    – splash58
    Jan 2 at 13:09
















0















user 1 writes a word (word1) and I want to count how many letters of the word1 there are within the opponent word (word2).
At first I used the strpos function but it doesn't seem to count reppeated letters so I change to the str_word_count function but no matter what the word1 is the countex is always 5
This is my code:



 for($i=0;$i<10 && ($rowword=mysqli_fetch_assoc($result)) ;$i++){

$aword1 = str_split($rowword['word']);
$word2=$row['word2']; //$row comes from another query that gets the word of the opponent
$countex=0;

for($i=0;$i<5;$i++){
$currentletter=$aword1[$i];

if(str_word_count($word2,0,$currentletter)){
$countex++;
}
}

} //end of outter for


Can someone tell me what is the problem?



Example of what I want to achieve:



word2=annoy, word1=again
in this example countex should be 2 because in the word "again" the letter "a" and "n" exist in the word "annoy"










share|improve this question

























  • What a result do you expect if any letter is present some times in a word? May be, you show some examples?

    – splash58
    Jan 2 at 13:09














0












0








0








user 1 writes a word (word1) and I want to count how many letters of the word1 there are within the opponent word (word2).
At first I used the strpos function but it doesn't seem to count reppeated letters so I change to the str_word_count function but no matter what the word1 is the countex is always 5
This is my code:



 for($i=0;$i<10 && ($rowword=mysqli_fetch_assoc($result)) ;$i++){

$aword1 = str_split($rowword['word']);
$word2=$row['word2']; //$row comes from another query that gets the word of the opponent
$countex=0;

for($i=0;$i<5;$i++){
$currentletter=$aword1[$i];

if(str_word_count($word2,0,$currentletter)){
$countex++;
}
}

} //end of outter for


Can someone tell me what is the problem?



Example of what I want to achieve:



word2=annoy, word1=again
in this example countex should be 2 because in the word "again" the letter "a" and "n" exist in the word "annoy"










share|improve this question
















user 1 writes a word (word1) and I want to count how many letters of the word1 there are within the opponent word (word2).
At first I used the strpos function but it doesn't seem to count reppeated letters so I change to the str_word_count function but no matter what the word1 is the countex is always 5
This is my code:



 for($i=0;$i<10 && ($rowword=mysqli_fetch_assoc($result)) ;$i++){

$aword1 = str_split($rowword['word']);
$word2=$row['word2']; //$row comes from another query that gets the word of the opponent
$countex=0;

for($i=0;$i<5;$i++){
$currentletter=$aword1[$i];

if(str_word_count($word2,0,$currentletter)){
$countex++;
}
}

} //end of outter for


Can someone tell me what is the problem?



Example of what I want to achieve:



word2=annoy, word1=again
in this example countex should be 2 because in the word "again" the letter "a" and "n" exist in the word "annoy"







php string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 13:54







user1264

















asked Jan 2 at 13:04









user1264user1264

163




163













  • What a result do you expect if any letter is present some times in a word? May be, you show some examples?

    – splash58
    Jan 2 at 13:09



















  • What a result do you expect if any letter is present some times in a word? May be, you show some examples?

    – splash58
    Jan 2 at 13:09

















What a result do you expect if any letter is present some times in a word? May be, you show some examples?

– splash58
Jan 2 at 13:09





What a result do you expect if any letter is present some times in a word? May be, you show some examples?

– splash58
Jan 2 at 13:09












2 Answers
2






active

oldest

votes


















1














If you want to know how many letters are in both words then you can use this code:



$lettersFromWord1 = str_split ($word1);
$lettersFromWord2 = str_split ($word2);

$matches = 0;
foreach($lettersFromWord1 as $letter){
if ( in_array($letter, $lettersFromWord2)){
$matches++;
}
}

echo "Word: " . $word1 . " and Word: " . $word2 . " has " . $matches . " matching letters";





share|improve this answer































    0














    You can use str_split aswell as strpos for that:



    https://3v4l.org/7ogrE



    <?php

    $word1 = 'abcdefghijkl';
    $word2 = 'acgikluuo';
    $expected = 6;

    function countMatchingChars($word1, $word2) {
    $matching = 0;
    foreach(str_split($word1) as $char) {
    if(strpos($word2, $char) !== false) {
    $matching++;
    }
    }

    return $matching;
    }

    $matchingChars = countMatchingChars($word1, $word2);

    assert($matchingChars === $expected);


    References:



    http://php.net/str_split



    http://php.net/strpos






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006895%2fusing-str-word-count-to-count-letters-within-the-opponent-word%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      If you want to know how many letters are in both words then you can use this code:



      $lettersFromWord1 = str_split ($word1);
      $lettersFromWord2 = str_split ($word2);

      $matches = 0;
      foreach($lettersFromWord1 as $letter){
      if ( in_array($letter, $lettersFromWord2)){
      $matches++;
      }
      }

      echo "Word: " . $word1 . " and Word: " . $word2 . " has " . $matches . " matching letters";





      share|improve this answer




























        1














        If you want to know how many letters are in both words then you can use this code:



        $lettersFromWord1 = str_split ($word1);
        $lettersFromWord2 = str_split ($word2);

        $matches = 0;
        foreach($lettersFromWord1 as $letter){
        if ( in_array($letter, $lettersFromWord2)){
        $matches++;
        }
        }

        echo "Word: " . $word1 . " and Word: " . $word2 . " has " . $matches . " matching letters";





        share|improve this answer


























          1












          1








          1







          If you want to know how many letters are in both words then you can use this code:



          $lettersFromWord1 = str_split ($word1);
          $lettersFromWord2 = str_split ($word2);

          $matches = 0;
          foreach($lettersFromWord1 as $letter){
          if ( in_array($letter, $lettersFromWord2)){
          $matches++;
          }
          }

          echo "Word: " . $word1 . " and Word: " . $word2 . " has " . $matches . " matching letters";





          share|improve this answer













          If you want to know how many letters are in both words then you can use this code:



          $lettersFromWord1 = str_split ($word1);
          $lettersFromWord2 = str_split ($word2);

          $matches = 0;
          foreach($lettersFromWord1 as $letter){
          if ( in_array($letter, $lettersFromWord2)){
          $matches++;
          }
          }

          echo "Word: " . $word1 . " and Word: " . $word2 . " has " . $matches . " matching letters";






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 13:21









          JohnnyB1988JohnnyB1988

          1467




          1467

























              0














              You can use str_split aswell as strpos for that:



              https://3v4l.org/7ogrE



              <?php

              $word1 = 'abcdefghijkl';
              $word2 = 'acgikluuo';
              $expected = 6;

              function countMatchingChars($word1, $word2) {
              $matching = 0;
              foreach(str_split($word1) as $char) {
              if(strpos($word2, $char) !== false) {
              $matching++;
              }
              }

              return $matching;
              }

              $matchingChars = countMatchingChars($word1, $word2);

              assert($matchingChars === $expected);


              References:



              http://php.net/str_split



              http://php.net/strpos






              share|improve this answer




























                0














                You can use str_split aswell as strpos for that:



                https://3v4l.org/7ogrE



                <?php

                $word1 = 'abcdefghijkl';
                $word2 = 'acgikluuo';
                $expected = 6;

                function countMatchingChars($word1, $word2) {
                $matching = 0;
                foreach(str_split($word1) as $char) {
                if(strpos($word2, $char) !== false) {
                $matching++;
                }
                }

                return $matching;
                }

                $matchingChars = countMatchingChars($word1, $word2);

                assert($matchingChars === $expected);


                References:



                http://php.net/str_split



                http://php.net/strpos






                share|improve this answer


























                  0












                  0








                  0







                  You can use str_split aswell as strpos for that:



                  https://3v4l.org/7ogrE



                  <?php

                  $word1 = 'abcdefghijkl';
                  $word2 = 'acgikluuo';
                  $expected = 6;

                  function countMatchingChars($word1, $word2) {
                  $matching = 0;
                  foreach(str_split($word1) as $char) {
                  if(strpos($word2, $char) !== false) {
                  $matching++;
                  }
                  }

                  return $matching;
                  }

                  $matchingChars = countMatchingChars($word1, $word2);

                  assert($matchingChars === $expected);


                  References:



                  http://php.net/str_split



                  http://php.net/strpos






                  share|improve this answer













                  You can use str_split aswell as strpos for that:



                  https://3v4l.org/7ogrE



                  <?php

                  $word1 = 'abcdefghijkl';
                  $word2 = 'acgikluuo';
                  $expected = 6;

                  function countMatchingChars($word1, $word2) {
                  $matching = 0;
                  foreach(str_split($word1) as $char) {
                  if(strpos($word2, $char) !== false) {
                  $matching++;
                  }
                  }

                  return $matching;
                  }

                  $matchingChars = countMatchingChars($word1, $word2);

                  assert($matchingChars === $expected);


                  References:



                  http://php.net/str_split



                  http://php.net/strpos







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 14:04









                  XatenevXatenev

                  5,82721235




                  5,82721235






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006895%2fusing-str-word-count-to-count-letters-within-the-opponent-word%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      How to fix TextFormField cause rebuild widget in Flutter