...

View Full Version : Can't match all characters...



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

Fou-Lu
04-20-2012, 02:50 PM
You have used strpos. PHP is a 0 based language, so if strpos finds a match on the first character is it position 0.
Change the conditional to if (false !== ($pos = strpos($word, $chars[$i], 0))) and that should work.

martynball
04-20-2012, 02:56 PM
I see, well I have made that change but this still prints out:


$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 )

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
8: Array ( [0] => f ) Removed: f


So it is saying it has removed the Array element but it is actually still there :S

It should look like this:


$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 )

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
8: Array ( ) Removed: f

tipsmail7
04-20-2012, 03:03 PM
You unset the array at wrong index

instead this

unset($word_split[$i]);
you should

unset($word_split[$pos]);

martynball
04-20-2012, 03:09 PM
Oh crap yeah, damn I make some stupid mistakes lol

Fou-Lu
04-20-2012, 03:25 PM
I just realized that this is the really long approach to this problem. You can simply pull the difference off of the word to chars arrays:


while($row = mysql_fetch_array($result))
{
$word = $row['word'];
$word_split = str_split($word);
$diff = array_diff($word_split, $chars);
if (empty($diff))
{
$con_words[] = $word;
}
}

martynball
04-20-2012, 03:49 PM
Cheers, I didn't know about that function.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum