Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-11-2013, 02:04 AM   PM User | #1
martynball
Regular Coder

 
Join Date: Nov 2007
Posts: 553
Thanks: 231
Thanked 0 Times in 0 Posts
martynball is an unknown quantity at this point
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);

martynball is offline   Reply With Quote
Old 02-11-2013, 11:21 AM   PM User | #2
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
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";
gvre is offline   Reply With Quote
Users who have thanked gvre for this post:
martynball (02-11-2013)
Old 02-11-2013, 11:37 AM   PM User | #3
martynball
Regular Coder

 
Join Date: Nov 2007
Posts: 553
Thanks: 231
Thanked 0 Times in 0 Posts
martynball is an unknown quantity at this point
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.
martynball is offline   Reply With Quote
Old 02-11-2013, 11:49 AM   PM User | #4
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
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);
}
gvre is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:43 AM.


Advertisement
Log in to turn off these ads.