Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post

    Can't match all characters...

    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.

    PHP Code:
    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:
    Code:
    $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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    martynball (04-20-2012)

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    I see, well I have made that change but this still prints out:
    Code:
    $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:
    Code:
    $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

  • #4
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post
    You unset the array at wrong index

    instead this
    PHP Code:
    unset($word_split[$i]); 
    you should
    PHP Code:
    unset($word_split[$pos]); 

  • Users who have thanked tipsmail7 for this post:

    martynball (04-20-2012)

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Oh crap yeah, damn I make some stupid mistakes lol

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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:
    PHP Code:
    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;
        }


  • The Following 2 Users Say Thank You to Fou-Lu For This Useful Post:

    martynball (04-20-2012), tipsmail7 (04-20-2012)

  • #7
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Cheers, I didn't know about that function.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •