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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post

    preg_match issue

    Hey, i'm try to search a list of words for matches using afew characters.

    So for example I will have the word "phone" in this list, and then I have the characters "hpnoe" which will spell phone in the correct order.

    But it seams my expression "[hpnoe]" is not matching "phone" unless the letters are in the correct order. Anyone know why?

    PHP Code:
    $matches = array();
    foreach (
    $pos_words as $word) {
        
    preg_match('['.$chars.']'$word$matches);


  • #2
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    If you want to check if chars in any order completely match word, try the following.

    PHP Code:
    <?php
    $word 
    preg_split('##''phone');
    sort($word);
    $word implode(''$word);

    $chars preg_split('##''hpnoe');
    sort($chars);
    $chars implode(''$chars);

    if (
    $word === $chars)
            echo 
    "match";
    else
            echo 
    "not match";

  • Users who have thanked gvre for this post:

    martynball (02-11-2013)

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    The phone word was just an example, I can see what you have done there but in reality I won't know what the word is, as basically I query about 14 characters "a-z" against a dictionary, and define the length of the word.

    Then I need it to return possible words that can be made with the 14 characters.

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    Try this

    PHP Code:
    <?php
    $words 
    = array('phone''pheon''test''whatever'); // words from db
    $chars 'hpnoe'// random chars

    $chars preg_split('##'$chars);
    sort($chars);
    $chars implode(''$chars);

    $matches = array();
    foreach(
    $words as $word)
    {
            
    $origWord $word;

            
    $word preg_split('##'$word);
            
    sort($word);
            
    $word implode(''$word);
            if (
    $word === $chars)
                    
    $matches[] = $origWord;
    }

    if (isset(
    $matches[0]))
    {
            
    print_r($matches);
    }


  •  

    Posting Permissions

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