Using str_word_count to count letters within the opponent word
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
add a comment |
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
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
add a comment |
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
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
php string
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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";
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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";
add a comment |
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";
add a comment |
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";
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";
answered Jan 2 at 13:21
JohnnyB1988JohnnyB1988
1467
1467
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 2 at 14:04


XatenevXatenev
5,82721235
5,82721235
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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