martynball
04-20-2012, 01:27 PM
Having another issue here, I can't see the problem. Basically the loop should check each character of an array against a word, if there is a match then remove that character from the word (in a separate Array).
It is working fine, apart from the character "f" isn't being detected :S This is the first letter of the word, which I figure is an issue. Also "flower" is just a test word, first thing that came to mind lol.
echo "\$chars: ";
print_r($chars); echo "<br /><br />";
//Start fecthing database data
$query = "SELECT * FROM dictionary WHERE LENGTH(word) = ".$length;
$result = mysql_query($query);
if(!$result) { $return_data = "Error connecting to dictionary!"; }
//Fetch word matching the selected length
while($row = mysql_fetch_array($result)) {
$word = $row['word']; $word_split = str_split($word);
echo "Current word: ".$word."<br /><br />"; //Ignore, for debugging
for ($i = 0; $i < count($chars); $i++) {
if ($pos = strpos($word, $chars[$i], 0)) {
unset($word_split[$i]);
echo $i.": "; //Ignore, for debugging
print_r($word_split); //Ignore, for debugging
echo " Removed: ".$chars[$i]."<br />"; //Ignore, for debugging
}
}
//Add word to array if characters match
if (count($word_split) < 0) {
$con_words[] = $row['word'];
}
}
Here are some results printed on the screen so you can see a bit more what is going on:
$chars: Array ( [0] => g [1] => l [2] => o [3] => w [4] => e [5] => r [6] => g [7] => a [8] => f [9] => m [10] => x [11] => p )
Current word: flower
1: Array ( [0] => f [2] => o [3] => w [4] => e [5] => r ) Removed: l
2: Array ( [0] => f [3] => w [4] => e [5] => r ) Removed: o
3: Array ( [0] => f [4] => e [5] => r ) Removed: w
4: Array ( [0] => f [5] => r ) Removed: e
5: Array ( [0] => f ) Removed: r
It is working fine, apart from the character "f" isn't being detected :S This is the first letter of the word, which I figure is an issue. Also "flower" is just a test word, first thing that came to mind lol.
echo "\$chars: ";
print_r($chars); echo "<br /><br />";
//Start fecthing database data
$query = "SELECT * FROM dictionary WHERE LENGTH(word) = ".$length;
$result = mysql_query($query);
if(!$result) { $return_data = "Error connecting to dictionary!"; }
//Fetch word matching the selected length
while($row = mysql_fetch_array($result)) {
$word = $row['word']; $word_split = str_split($word);
echo "Current word: ".$word."<br /><br />"; //Ignore, for debugging
for ($i = 0; $i < count($chars); $i++) {
if ($pos = strpos($word, $chars[$i], 0)) {
unset($word_split[$i]);
echo $i.": "; //Ignore, for debugging
print_r($word_split); //Ignore, for debugging
echo " Removed: ".$chars[$i]."<br />"; //Ignore, for debugging
}
}
//Add word to array if characters match
if (count($word_split) < 0) {
$con_words[] = $row['word'];
}
}
Here are some results printed on the screen so you can see a bit more what is going on:
$chars: Array ( [0] => g [1] => l [2] => o [3] => w [4] => e [5] => r [6] => g [7] => a [8] => f [9] => m [10] => x [11] => p )
Current word: flower
1: Array ( [0] => f [2] => o [3] => w [4] => e [5] => r ) Removed: l
2: Array ( [0] => f [3] => w [4] => e [5] => r ) Removed: o
3: Array ( [0] => f [4] => e [5] => r ) Removed: w
4: Array ( [0] => f [5] => r ) Removed: e
5: Array ( [0] => f ) Removed: r