1,105,402 Community Members

Matching Letters In An Array

Member Avatar
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm trying to simply match these letters using php, but it's kind-of a complex matching formula...

First, I have two arrays and one string. The arrays contain letters and the string is a near match of those letters.

$a - [Array] Must be <= 7
$b - [Array] Must be >= 0
$word - [String] Must contain letters from $a & $b. Letters can't be used twice.

The Goal: Create a word from those letters with complex matching. To start each word must contain one and only one $b letter, unless no $b letters are given. Next it must make sure that $word only contains letters from $a (it can't use letters twice but $a can contain the same letter) (and of course one or zero $b letters).

Example #1:
$a = R-E-O-E
$b = E-P
$word = Red
(The code should look at this an not accept it because $a nor $b doesn't contain a D)

Example #2:
$a = R-E-O-E
$b = E-P
$word = Peer
(The code should accept this because $b contains P and $a contains two E's and an R)

Note: This will be in a long loop with many different words.

Any help will be greatly appreciated!

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Try this:

<?php

$a = array('test','text','ext','tex','deer'); # words to filter
$b = array('e','s','t','d','r'); # letters to check

function c($letter,$array)
{
        $n = strlen($letter);
        $r = array();
        for($i = 0; $i < $n; $i++)
        {
                if(in_array($letter[$i],$array))
                {
                        $r[] = true;
                }
                else
                {
                        $r[] = false;
                }
        }
        return (in_array(null,$r)) ? false:true;
}

foreach($a as $word)
{
        echo $word .': '. c($word,$b) . "\n";
}
?>

bye.

Member Avatar
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Try this:

<?php

$a = array('test','text','ext','tex','deer'); # words to filter
$b = array('e','s','t','d','r'); # letters to check

function c($letter,$array)
{
        $n = strlen($letter);
        $r = array();
        for($i = 0; $i < $n; $i++)
        {
                if(in_array($letter[$i],$array))
                {
                        $r[] = true;
                }
                else
                {
                        $r[] = false;
                }
        }
        return (in_array(null,$r)) ? false:true;
}

foreach($a as $word)
{
        echo $word .': '. c($word,$b) . "\n";
}
?>

bye.

Great suggestion, but that isn't really what I'm looking for. If you look back at my example I explain two different arrays and one word.

Thanks for trying!

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

I'm sorry, now it should work like you want:

<?php
$a = array('e','x','t','d','a');
$b = array('r','g','n');
$word = 'extra'; # try "gextra" for example, because of g and r it will give false

function c($word,$array,$barray)
{
	$n = strlen($word);
	$r = array();
	$bn = 0;
	for($i = 0; $i < $n; $i++)
	{
		if(in_array($word[$i],$array))
		{
			$r[$i] = true;
		}
		else
		{
			$r[$i] = false;
		}

		if(in_array($word[$i],$barray))
		{
			if($bn == 0)
			{
				$bn = 1;
				$r[$i] = true;
			}
			else
			{
				$r[$i] = false;
			}
		}
	}

	return (in_array(null,$r)) ? false:true;
}

echo $word .': '. c($word,$a,$b) . "\n";
echo "\n";
?>

bye :)

Member Avatar
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm sorry, now it should work like you want:

<?php
$a = array('e','x','t','d','a');
$b = array('r','g','n');
$word = 'extra'; # try "gextra" for example, because of g and r it will give false

function c($word,$array,$barray)
{
	$n = strlen($word);
	$r = array();
	$bn = 0;
	for($i = 0; $i < $n; $i++)
	{
		if(in_array($word[$i],$array))
		{
			$r[$i] = true;
		}
		else
		{
			$r[$i] = false;
		}

		if(in_array($word[$i],$barray))
		{
			if($bn == 0)
			{
				$bn = 1;
				$r[$i] = true;
			}
			else
			{
				$r[$i] = false;
			}
		}
	}

	return (in_array(null,$r)) ? false:true;
}

echo $word .': '. c($word,$a,$b) . "\n";
echo "\n";
?>

bye :)

You're smart, but not quite there yet! For example, I tried the word "exta" which contains no letters from the $b array. Each word must contain letters from the $b array unless no letters are in the $b array.

Another problem is if I do the word "exxtra" it contains two x's, but the $a array only contains one x.

Thanks!

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Try it and let me know, I've tried to match your examples..

<?php

# count and compare values between array a and word 
function d($word, $array)
{
	global $d;
	$u = array_unique($array);
	$w = str_split($word);
	
	$u2 = array_intersect($u,$w);
	$a1 = array_count_values($array);
	$w1 = array_count_values($w);
	$a = array();
	foreach($u2 as $k => $v)
	{
    	$a[] = ($a1[$v] >= $w1[$v]) ? true:false;
	}

	$d = in_array(false,$a) ? false:true;
	return $d;
}

# check array b
function e($word,$barray)
{    
	global $e;
	$r = array();
	$n = str_split($word);
	$bn = 0;
	if(count($barray) != 0 && is_array($barray))
	{
		for($i = 0; $i < count($n); $i++)
		{
			if(in_array($n[$i],$barray))
			{
				if($bn == 0)
				{
					$bn++;
					$r[] = 1;
				}
				else
				{
					$bn++;
					$r[] = 2;
				}
			}
			else
			{
				$r[] = 3;
			}
		}
	}
	elseif(count($barray) == 0 && is_array($barray))
	{
	    $r[] = 1;
	}
	else
	{
	    $r[] = 0;
	}
	
	# codes:
	# 0 no array
	# 1 array yes, letter matched or empty array
	# 2 array yes, more than one letter matched
	# 3 array yes, no letter
	
	$e = (in_array(1,$r) == true && in_array(2,$r) == false) ? true:false;
	return $e;
}

# check array a
function c($word,$array)
{
	global $d;
	global $e;
	$n = str_split($word);
	$r = array();
	for($i = 0; $i < count($n); $i++)
	{
		if(in_array($n[$i],$array))
		{
			$r[] = true;
		}
		elseif($e == true)
		{
		    $r[] = true;
		}
		else
		{
			$r[] = false;
		}		
	}
	
	$res = array();
	$res[] = $d;
	$res[] = $e;
	$res[] = in_array(false,$r) ? false:true;
	return in_array(false,$res) ? 'false':'true';
}

$a = array('x','x','e','t','d','a');
$b = array('r','g','n');
#$b = array(); # empty array
$word = 'exxxta';

d($word,$a);
e($word,$b);
print_r(c($word,$a));
?>

hope I got it this time.. :D

Member Avatar
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks Ceral, I'm not quite sure if that worked. Here is all of my code. Calm down. I know it's messy...

<?php

if(empty($_POST['yl']))
{
    echo "<form action='index.php' method='POST'>Your Letters: <input type='text' name='yl'>Board Letters: <input type='text' name='bl'><input type='submit'></form>";
}
else
{

$a = preg_split('//', $_POST['yl']);
$b = preg_split('//', $_POST['bl']);

function pc_permute($items, $perms = array( )) {
    if (empty($items)) {
        
        $workWords = array();
        
        $a = array('g','p');
        $b = array('o');

        $w = join('', $perms);
	$pspell_link = pspell_new("en"); # set language
 
	if (pspell_check($pspell_link, strtolower($w))) {

	}
 

	else
	{
	    $suggestions = pspell_suggest($pspell_link, $w);
            foreach ($suggestions as $suggestion)
            {
                d($suggestion,$a);
                e($suggestion,$b);
                
                if(c($suggestion,$a) == "true")
                {
                    if(in_array($suggestion,$wordWorks))
                    {
                        //do nothing
                    }
                    else
                    {
                    $wordWorks[] = $suggestion;
                    echo $suggestion."<br>";
                    }
                }
                
            }
	}
 
    }  else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
	     usleep(2000); # slow down for CPU
             $newitems = $items;
             $newperms = $perms;
             list($foo) = array_splice($newitems, $i, 1);
             array_unshift($newperms, $foo);
             pc_permute($newitems, $newperms); # recursive loop
         }
    }

return $workWords;

}
 


 
echo "\n";

# count and compare values between array a and word 
function d($word, $array)
{
	global $d;
	$u = array_unique($array);
	$w = str_split($word);
 
	$u2 = array_intersect($u,$w);
	$a1 = array_count_values($array);
	$w1 = array_count_values($w);
	$a = array();
	foreach($u2 as $k => $v)
	{
    	$a[] = ($a1[$v] >= $w1[$v]) ? true:false;
	}
 
	$d = in_array(false,$a) ? false:true;
	return $d;
}
 
# check array b
function e($word,$barray)
{    
	global $e;
	$r = array();
	$n = str_split($word);
	$bn = 0;
	if(count($barray) != 0 && is_array($barray))
	{
		for($i = 0; $i < count($n); $i++)
		{
			if(in_array($n[$i],$barray))
			{
				if($bn == 0)
				{
					$bn++;
					$r[] = 1;
				}
				else
				{
					$bn++;
					$r[] = 2;
				}
			}
			else
			{
				$r[] = 3;
			}
		}
	}
	elseif(count($barray) == 0 && is_array($barray))
	{
	    $r[] = 1;
	}
	else
	{
	    $r[] = 0;
	}
 
	# codes:
	# 0 no array
	# 1 array yes, letter matched or empty array
	# 2 array yes, more than one letter matched
	# 3 array yes, no letter
 
	$e = (in_array(1,$r) == true && in_array(2,$r) == false) ? true:false;
	return $e;
}
 
# check array a
function c($word,$array)
{
	global $d;
	global $e;
	$n = str_split($word);
	$r = array();
	for($i = 0; $i < count($n); $i++)
	{
		if(in_array($n[$i],$array))
		{
			$r[] = true;
		}
		elseif($e == true)
		{
		    $r[] = true;
		}
		else
		{
			$r[] = false;
		}		
	}
 
	$res = array();
	$res[] = $d;
	$res[] = $e;
	$res[] = in_array(false,$r) ? false:true;
	return in_array(false,$res) ? 'false':'true';
}

$c = array_merge($a,$b);
pc_permute($c); # run
}

?>

Okay, now here is what's being returned... (It won't stop loading)

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/joeb34/public_html/Scrabble-Cheater/1-0/index.php on line 54
holster
Holst
honester
holsters
holstered
construe
honest
Holst's
holster's

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/joeb34/public_html/Scrabble-Cheater/1-0/index.php on line 54
holster
Holst
honester
holsters
holstered
construe
honest
Holst's
holster's

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/joeb34/public_html/Scrabble-Cheater/1-0/index.php on line 54
holster
honester
Holst
holsters
holstered
honest
holster's

Any help would highly be appreciated!

Member Avatar
diafol
Where are my eyes?
12,949 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,846 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

Looks like an infinite loop.

for ($i = count($items) - 1; $i >= 0; --$i) {

It's not reaching -1.

Perhaps echoing $i in the loop will show you what's going on. Maybe:

for ($i = count($items) - 1; $i >= 0; $i--) {
Member Avatar
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Looks like an infinite loop.

for ($i = count($items) - 1; $i >= 0; --$i) {

It's not reaching -1.

Perhaps echoing $i in the loop will show you what's going on. Maybe:

for ($i = count($items) - 1; $i >= 0; $i--) {

Okay I added the echo $i, but it still never seems to end and it looks like this...

6543210
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/joeb34/public_html/Scrabble-Cheater/1-0/index.php on line 54
you
yo
Yugo
duo
00
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/joeb34/public_html/Scrabble-Cheater/1-0/index.php on line 54
you
yo
Yugo
duo
110
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/joeb34/public_html/Scrabble-Cheater/1-0/index.php on line 54
yo
Wyo
Ugo
00
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/joeb34/public_html/Scrabble-Cheater/1-0/index.php on line 54
yo
Wyo
Ugo
010
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/joeb34/public_html/Scrabble-Cheater/1-0/index.php on line 54
you
yo
Yugo
duo
00
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/joeb34/public_html/Scrabble-Cheater/1-0/index.php on line 54
yo
Wyo
Ugo
2210

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

This works without errors, unless input is too long and the execution time goes over 30 seconds, at that point PHP will stop the script and return part of the output and an error. For this reason I added a length check at the end of the script, otherwise you have to increase max_execution_time.

<?php

if(empty($_POST['yl']))
{
    echo "<form action='index.php' method='POST'>
    Your Letters: <input type='text' name='yl' value='oor'>
    Board Letters: <input type='text' name='bl' value='dt'>
    <input type='submit'></form>";
}
else
{
    $replace = array(',',' ','','-');
    $a = str_split(str_replace($replace,'',$_POST['yl']));
    $b = str_split(str_replace($replace,'',$_POST['bl']));

    # removable
    echo '<h5>input</h5><pre>';
    print_r($a);
    print_r($b);
    echo '</pre>';

    function pc_permute($items, $perms = array( )) {

        global $a;
        global $b;
        global $d;
        global $e;
        
        if (empty($items)) {
            $w = join('', $perms);
	        $pspell_link = pspell_new("en"); # set language

	        $suggestions = pspell_suggest($pspell_link, $w);
	        $z = array();
                foreach ($suggestions as $suggestion)
                {
                    $s = strtolower($suggestion);
                    d($s,$a);
                    e($s,$b);
                    
                    if(c($s,$a) == "true")
                    {
                        $z[] = $s;
                    }
                    
                }
                $z2 = array_unique($z);
                foreach($z2 as $wrd)
                {
                    echo $wrd . "<br />";
                }
                echo '<hr />';
     
        }  else {
            for ($i = count($items) - 1; $i >= 0; --$i) {
	         usleep(2000); # slow down for CPU
                 $newitems = $items;
                 $newperms = $perms;
                 list($foo) = array_splice($newitems, $i, 1);
                 array_unshift($newperms, $foo);
                 pc_permute($newitems, $newperms); # recursive loop
             }
        }

    }

    # count and compare values between array a and word 
    function d($word, $array)
    {
	    global $d;
	    $u = array_unique($array);
	    $w = str_split($word);
     
	    $u2 = array_intersect($u,$w);
	    $a1 = array_count_values($array);
	    $w1 = array_count_values($w);
	    $a = array();
	    foreach($u2 as $k => $v)
	    {
        	$a[] = ($a1[$v] >= $w1[$v]) ? true:false;
	    }
     
	    $d = in_array(false,$a) ? false:true;
	    return $d;
    }
     
    # check array b
    function e($word,$barray)
    {    
	    global $e;
	    $r = array();
	    $n = str_split($word);
	    $bn = 0;
	    if(count($barray) != 0 && is_array($barray))
	    {
		    for($i = 0; $i < count($n); $i++)
		    {
			    if(in_array($n[$i],$barray))
			    {
				    if($bn == 0)
				    {
					    $bn++;
					    $r[] = 1;
				    }
				    else
				    {
					    $bn++;
					    $r[] = 2;
				    }
			    }
			    else
			    {
				    $r[] = 3;
			    }
		    }
	    }
	    elseif(count($barray) == 0 && is_array($barray))
	    {
	        $r[] = 1;
	    }
	    else
	    {
	        $r[] = 0;
	    }
     
	    # codes:
	    # 0 no array
	    # 1 array yes, letter matched or empty array
	    # 2 array yes, more than one letter matched
	    # 3 array yes, no letter
     
	    $e = (in_array(1,$r) == true && in_array(2,$r) == false) ? true:false;
	    return $e;
    }

    # check array a
    function c($word,$array)
    {
	    global $d;
	    global $e;
	    $n = str_split($word);
	    $r = array();
	    for($i = 0; $i < count($n); $i++)
	    {
		    if(in_array($n[$i],$array))
		    {
			    $r[] = true;
		    }
		    elseif($e == true)
		    {
		        $r[] = true;
		    }
		    else
		    {
			    $r[] = false;
		    }		
	    }
     
	    $res = array();
	    $res[] = $d;
	    $res[] = $e;
	    $res[] = in_array(false,$r) ? false:true;
	    return in_array(false,$res) ? 'false':'true';
    }

    $c = array_merge($a,$b);
    if(count($c) > 5) # check length
    {
        echo 'too long';
    }
    else
    {
        pc_permute($c); # run also with pc_permute($a,$b)
    }
}

?>

Is this the output you are searching for? I'm still in doubt.. hehe :D

P.S. also consider for Pritaeas suggestion, something in C# or C++ may be hugely faster than PHP: http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/408526/1748151#post1748151

Member Avatar
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

This works without errors, unless input is too long and the execution time goes over 30 seconds, at that point PHP will stop the script and return part of the output and an error. For this reason I added a length check at the end of the script, otherwise you have to increase max_execution_time.

<?php

if(empty($_POST['yl']))
{
    echo "<form action='index.php' method='POST'>
    Your Letters: <input type='text' name='yl' value='oor'>
    Board Letters: <input type='text' name='bl' value='dt'>
    <input type='submit'></form>";
}
else
{
    $replace = array(',',' ','','-');
    $a = str_split(str_replace($replace,'',$_POST['yl']));
    $b = str_split(str_replace($replace,'',$_POST['bl']));

    # removable
    echo '<h5>input</h5><pre>';
    print_r($a);
    print_r($b);
    echo '</pre>';

    function pc_permute($items, $perms = array( )) {

        global $a;
        global $b;
        global $d;
        global $e;
        
        if (empty($items)) {
            $w = join('', $perms);
	        $pspell_link = pspell_new("en"); # set language

	        $suggestions = pspell_suggest($pspell_link, $w);
	        $z = array();
                foreach ($suggestions as $suggestion)
                {
                    $s = strtolower($suggestion);
                    d($s,$a);
                    e($s,$b);
                    
                    if(c($s,$a) == "true")
                    {
                        $z[] = $s;
                    }
                    
                }
                $z2 = array_unique($z);
                foreach($z2 as $wrd)
                {
                    echo $wrd . "<br />";
                }
                echo '<hr />';
     
        }  else {
            for ($i = count($items) - 1; $i >= 0; --$i) {
	         usleep(2000); # slow down for CPU
                 $newitems = $items;
                 $newperms = $perms;
                 list($foo) = array_splice($newitems, $i, 1);
                 array_unshift($newperms, $foo);
                 pc_permute($newitems, $newperms); # recursive loop
             }
        }

    }

    # count and compare values between array a and word 
    function d($word, $array)
    {
	    global $d;
	    $u = array_unique($array);
	    $w = str_split($word);
     
	    $u2 = array_intersect($u,$w);
	    $a1 = array_count_values($array);
	    $w1 = array_count_values($w);
	    $a = array();
	    foreach($u2 as $k => $v)
	    {
        	$a[] = ($a1[$v] >= $w1[$v]) ? true:false;
	    }
     
	    $d = in_array(false,$a) ? false:true;
	    return $d;
    }
     
    # check array b
    function e($word,$barray)
    {    
	    global $e;
	    $r = array();
	    $n = str_split($word);
	    $bn = 0;
	    if(count($barray) != 0 && is_array($barray))
	    {
		    for($i = 0; $i < count($n); $i++)
		    {
			    if(in_array($n[$i],$barray))
			    {
				    if($bn == 0)
				    {
					    $bn++;
					    $r[] = 1;
				    }
				    else
				    {
					    $bn++;
					    $r[] = 2;
				    }
			    }
			    else
			    {
				    $r[] = 3;
			    }
		    }
	    }
	    elseif(count($barray) == 0 && is_array($barray))
	    {
	        $r[] = 1;
	    }
	    else
	    {
	        $r[] = 0;
	    }
     
	    # codes:
	    # 0 no array
	    # 1 array yes, letter matched or empty array
	    # 2 array yes, more than one letter matched
	    # 3 array yes, no letter
     
	    $e = (in_array(1,$r) == true && in_array(2,$r) == false) ? true:false;
	    return $e;
    }

    # check array a
    function c($word,$array)
    {
	    global $d;
	    global $e;
	    $n = str_split($word);
	    $r = array();
	    for($i = 0; $i < count($n); $i++)
	    {
		    if(in_array($n[$i],$array))
		    {
			    $r[] = true;
		    }
		    elseif($e == true)
		    {
		        $r[] = true;
		    }
		    else
		    {
			    $r[] = false;
		    }		
	    }
     
	    $res = array();
	    $res[] = $d;
	    $res[] = $e;
	    $res[] = in_array(false,$r) ? false:true;
	    return in_array(false,$res) ? 'false':'true';
    }

    $c = array_merge($a,$b);
    if(count($c) > 5) # check length
    {
        echo 'too long';
    }
    else
    {
        pc_permute($c); # run also with pc_permute($a,$b)
    }
}

?>

Is this the output you are searching for? I'm still in doubt.. hehe :D

P.S. also consider for Pritaeas suggestion, something in C# or C++ may be hugely faster than PHP: http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/408526/1748151#post1748151

Thanks for the reply. That still isn't what I'm looking for. You see, I don't really want the words to repeat. With your code here is the beginning of my output...

Notice that Bord and Bord repeats. I want no repeats...

input

Array
(
[0] => o
[1] => o
[2] => r
)
Array
(
[0] => d
)
bord
cord
ford
lord
mord
word
bord
cord
ford
lord
mord
word
rood
prod
rod
brod
trod

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Ok, I changed permutation function, it's taken always from that article: http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm
Previously it was hard to return the output from the function, now I created a class:

<?php
# filename perm.class.php
Class Perm
{

    public function start($arrayA,$arrayB)
    {
        $c = array_merge($arrayA,$arrayB);
    
        if(count($c) > 5)
        {
            return 'too long';
        }
        else
        {
            $size = count($c)-1;
            $perm = range(0, $size);
            $j = 0;

            do {
                 foreach ($perm as $i) { $perms[$j][] = $c[$i]; }
            } while ($perm = $this->p($perm, $size) and ++$j);

            $list = array();
            foreach ($perms as $p) {
                $list[] = join('', $p);
            }
            
            return array_unique($this->ps($list));
        }
    }
    
    # generate permutations
    private function p($p, $size) {
        for ($i = $size-1; @$p[$i] >= $p[$i+1]; --$i) { }
        if ($i == -1) { return false; }
        for ($j = $size; $p[$j] <= $p[$i]; --$j) { }

        $tmp = $p[$i];
        $p[$i] = $p[$j];
        $p[$j] = $tmp;

        for (++$i, $j = $size; $i < $j; ++$i, --$j) {
             $tmp = $p[$i];
             $p[$i] = $p[$j];
             $p[$j] = $tmp;
        }

        return $p;
    }

    # count and compare values between array a and word 
    private function d($word, $array)
    {
	    global $d;
	    $u = array_unique($array);
	    $w = str_split($word);
     
	    $u2 = array_intersect($u,$w);
	    $a1 = array_count_values($array);
	    $w1 = array_count_values($w);
	    $a = array();
	    foreach($u2 as $k => $v)
	    {
        	$a[] = ($a1[$v] >= $w1[$v]) ? true:false;
	    }
     
	    $d = in_array(false,$a) ? false:true;
	    return $d;
    }
     
    # check array b
    private function e($word,$barray)
    {    
	    global $e;
	    $r = array();
	    $n = str_split($word);
	    $bn = 0;
	    if(count($barray) != 0 && is_array($barray))
	    {
		    for($i = 0; $i < count($n); $i++)
		    {
			    if(in_array($n[$i],$barray))
			    {
				    if($bn == 0)
				    {
					    $bn++;
					    $r[] = 1;
				    }
				    else
				    {
					    $bn++;
					    $r[] = 2;
				    }
			    }
			    else
			    {
				    $r[] = 3;
			    }
		    }
	    }
	    elseif(count($barray) == 0 && is_array($barray))
	    {
	        $r[] = 1;
	    }
	    else
	    {
	        $r[] = 0;
	    }
     
	    # codes:
	    # 0 no array
	    # 1 array yes, letter matched or empty array
	    # 2 array yes, more than one letter matched
	    # 3 array yes, no letter
     
	    $e = (in_array(1,$r) == true && in_array(2,$r) == false) ? true:false;
	    return $e;
    }
    
    # removable, just test message
    public function mess()
    {
        return 'hello world';
    }

    # check array a
    private function c($word,$array)
    {
	    global $d;
	    global $e;
	    global $result;
	    $n = str_split($word);
	    $r = array();
	    for($i = 0; $i < count($n); $i++)
	    {
		    if(in_array($n[$i],$array))
		    {
			    $r[] = true;
		    }
		    elseif($e == true)
		    {
		        $r[] = true;
		    }
		    else
		    {
			    $r[] = false;
		    }		
	    }
     
	    $res = array();
	    $res[] = $d;
	    $res[] = $e;
	    $res[] = in_array(false,$r) ? false:true;
	    $result = in_array(false,$res) ? false:true;
	    return $result;
    }
    
    # check real words
    private function ps($list)
    {
        global $a;
        global $b;
        $n = count($list);
        $pspell_link = pspell_new("en"); # set language
        $z = array();
        for($i = 0; $i < $n; $i++)
        {
            $suggestions = pspell_suggest($pspell_link, $list[$i]);
            foreach ($suggestions as $suggestion)
            {
                if(strlen($suggestion) > 1)
                {
                    $s = strtolower($suggestion);
                    $this->d($s,$a);
                    $this->e($s,$b);
                      
                    if($this->c($s,$a) == true)
                    {
                        $z[] = $s;
                    }
                }
                    
            }        
        }
        return $z;
    }
}
?>

And this is the page where you have to include the class:

<?php
include 'perm.class.php'; # include class

# page outputs

if(empty($_POST['yl']))
{
    echo "<form action='n.php' method='POST'>
    Your Letters: <input type='text' name='yl' value='oor'>
    Board Letters: <input type='text' name='bl' value='dt'>
    <input type='submit'></form>";
}
else
{
    $replace = array(',',' ','','-');
    $a = str_split(str_replace($replace,'',$_POST['yl']));
    $b = str_split(str_replace($replace,'',$_POST['bl']));
    echo '<h5>input</h5><pre>';
    print_r($a);
    print_r($b);
    echo '</pre>';

    # start
    $play = new Perm();
    echo '<pre>';
    print_r($play->start($a,$b)); # you only need this to get results
    echo '</pre>';
}
?>

Files are attached, the only problem is:

Notice: Undefined offset: -1 in /var/www/test/m/perm.class.php on line 35

which is not shown because I placed a @ (booo on me), but right now I just can't get that straight.. too tired ^_^

Attachments perm.class_.php (3.91KB) n.php (0.62KB)
Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Just an add, on line 178 of the class you can change the conditional statement to:

if($this->c($s,$a) == true && ctype_alpha($s))

this will remove results like root's, odor's and the others.

Member Avatar
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Just an add, on line 178 of the class you can change the conditional statement to:

if($this->c($s,$a) == true && ctype_alpha($s))

this will remove results like root's, odor's and the others.

One last problem. If I don't put in a character in the board letters text box nothing comes back.

I would like (if there are no letters in the $b array) for it to find as many words as possible using just the $a array.

Great job!

Member Avatar
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

One last problem. If I don't put in a character in the board letters text box nothing comes back.

I would like (if there are no letters in the $b array) for it to find as many words as possible using just the $a array.

Great job!

Wait, it's not quite working any more. I put in this...

Your Letters: kswdh
Board Letters: o

And I got these results...

None of them work...

cosed - 8
justino - 14
gusto - 6
castro - 8
giusto - 7
castor - 8
ghost - 9
gestapo - 10
gustavo - 11
cost - 10
kudos - 4
sod - 7
isador - 7
isidor - 9
pseudo - 5
soda - 12
judo - 3
sot - 6
misdo - 7
scot - 16
stow - 16
guido - 13
cowhide - 15
cowherd - 16
suharto - 7
cuspidor - 9
coxed - 9
custody - 11
exhort - 10
costa - 8
caseload - 12
sword - 5
swot - 7
sowed - 11
keyword - 7
cowed - 13
resowed - 7
gaston - 16
custom - 14
stowe - 11
caxton - 6
keystone - 12
postwar - 7
guizot - 12
joist - 8
joust - 8

Another thing I changed was line 10 of the class...

if(count($c) > 100)

AND n.php...

<?php
include 'perm.class.php'; # include class
 
# page outputs

function scoreit($c,$x)
{
    if($c == "a")
    {
        $x = $x + 1;
    }
    
    if($c == "b")
    {
        $x = $x + 3;
    }
    
    if($c == "c")
    {
        $x = $x + 3;
    }
    
    if($c == "d")
    {
        $x = $x + 2;
    }
    
    if($c == "e")
    {
        $x = $x + 1;
    }
    
    if($c == "f")
    {
        $x = $x + 4;
    }
    
    if($c == "g")
    {
        $x = $x + 2;
    }
    if($c == "h")
    {
        $x = $x + 4;
    }
    
    if($c == "i")
    {
        $x = $x + 1;
    }
    
    if($c == "j")
    {
        $x = $x + 8;
    }
    
    if($c == "k")
    {
        $x = $x + 5;
    }
    
    if($c == "l")
    {
        $x = $x + 1;
    }
    
    if($c == "m")
    {
        $x = $x + 3;
    }
    
    if($c == "n")
    {
        $x = $x + 1;
    }
    
    if($c == "o")
    {
        $x = $x + 1;
    }
    
    if($c == "p")
    {
        $x = $x + 3;
    }
    
    if($c == "q")
    {
        $x = $x + 10;
    }
    
    if($c == "r")
    {
        $x = $x + 1;
    }
    
    if($c == "s")
    {
        $x = $x + 1;
    }
    
    if($c == "t")
    {
        $x = $x + 1;
    }
    
    if($c == "u")
    {
        $x = $x + 1;
    }
    
    if($c == "v")
    {
        $x = $x + 4;
    }
    
    if($c == "w")
    {
        $x = $x + 4;
    }
    
    if($c == "x")
    {
        $x = $x + 8;
    }
    
    if($c == "y")
    {
        $x = $x + 4;
    }
    
    if($c == "z")
    {
        $x = $x + 10;
    }
    
    return $x;
}
 
if(empty($_POST['yl']))
{

}
else
{
    $replace = array(',',' ','','-');
    $a = str_split(str_replace($replace,'',$_POST['yl']));
    $b = str_split(str_replace($replace,'',$_POST['bl']));
 
    # start
    $play = new Perm();
    echo '<pre>';
    $worknWords = $play->start($a,$b); # you only need this to get results
    $worknRatings = array();
    $x = 0;
    foreach($worknWords as $word)
    {
        $eachLetter = str_split($word);
        
        foreach($eachLetter as $char)
        {
            $x = scoreit($char,$x);
        }
        
        $worknRatings[] = $x;
        $x = 0; //reset
    }
    
    for($i = 0; $i < count($worknWords); $i++)
    {
        if($worknWords[$i] != "")
        {
            echo $worknWords[$i]." - ".$worknRatings[$i]."<br>";
        }
    }
    echo '</pre>';
}
?>
Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Wait, one problem at a time.
An update on the class:

<?php
Class Perm
{

    public function start($arrayA,$arrayB)
    {
        global $a;
        global $b;
        
        $replace = array(',',' ','','-');
        $a = str_split(str_replace($replace,'',$arrayA));
        
        if(empty($arrayB))
        {
            $bArr = false;
            $b = array();
        } else {
            $bArr = true;
            $b = str_split(str_replace($replace,'',$arrayB));
        }
        
        $c = ($bArr == false) ? $a : array_merge($a,$b);
        
        if(count($c) > 5)
        {
            return 'too long';
        }
        else
        {
            $size = count($c)-1;
            $perm = range(0, $size);
            $j = 0;

            do {
                 foreach ($perm as $i) { $perms[$j][] = $c[$i]; }
            } while ($perm = $this->p($perm, $size) and ++$j);

            $list = array();
            foreach ($perms as $p) {
                $list[] = join('', $p);
            }
            
            return array_unique($this->ps($list));
        }
    }
    
    # generate permutations
    private function p($p, $size) {
        for ($i = $size-1; @$p[$i] >= $p[$i+1]; --$i) { }
        if ($i == -1) { return false; }
        for ($j = $size; $p[$j] <= $p[$i]; --$j) { }

        $tmp = $p[$i];
        $p[$i] = $p[$j];
        $p[$j] = $tmp;

        for (++$i, $j = $size; $i < $j; ++$i, --$j) {
             $tmp = $p[$i];
             $p[$i] = $p[$j];
             $p[$j] = $tmp;
        }

        return $p;
    }

    # count and compare values between array a and word 
    private function d($word, $array)
    {
	    global $d;
	    $u = array_unique($array);
	    $w = str_split($word);
     
	    $u2 = array_intersect($u,$w);
	    $a1 = array_count_values($array);
	    $w1 = array_count_values($w);
	    $a = array();
	    foreach($u2 as $k => $v)
	    {
        	$a[] = ($a1[$v] >= $w1[$v]) ? true:false;
	    }
     
	    $d = in_array(false,$a) ? false:true;
	    return $d;
    }
     
    # check array b
    private function e($word,$barray)
    {    
	    global $e;
	    $r = array();
	    $n = str_split($word);
	    $bn = 0;
	    if(count($barray) != 0 && is_array($barray))
	    {
		    for($i = 0; $i < count($n); $i++)
		    {
			    if(in_array($n[$i],$barray))
			    {
				    if($bn == 0)
				    {
					    $bn++;
					    $r[] = 1;
				    }
				    else
				    {
					    $bn++;
					    $r[] = 2;
				    }
			    }
			    else
			    {
				    $r[] = 3;
			    }
		    }
	    }
	    elseif(count($barray) == 0 && is_array($barray))
	    {
	        $r[] = 1;
	    }
	    else
	    {
	        $r[] = 0;
	    }
     
	    # codes:
	    # 0 no array
	    # 1 array yes, letter matched or empty array
	    # 2 array yes, more than one letter matched
	    # 3 array yes, no letter
     
	    $e = (in_array(1,$r) == true && in_array(2,$r) == false) ? true:false;
	    return $e;
    }

    # check array a
    private function c($word,$array)
    {
	    global $d;
	    global $e;
	    global $result;
	    $n = str_split($word);
	    $r = array();
	    for($i = 0; $i < count($n); $i++)
	    {
		    if(in_array($n[$i],$array))
		    {
			    $r[] = true;
		    }
		    elseif($e == true)
		    {
		        $r[] = true;
		    }
		    else
		    {
			    $r[] = false;
		    }		
	    }
     
	    $res = array();
	    $res[] = $d;
	    $res[] = $e;
	    $res[] = in_array(false,$r) ? false:true;
	    $result = in_array(false,$res) ? false:true;
	    return $result;
    }
    
    # check real words
    private function ps($list)
    {
        global $a;
        global $b;
        $n = count($list);
        $pspell_link = pspell_new("en"); # set language
        $z = array();
        for($i = 0; $i < $n; $i++)
        {
            $suggestions = pspell_suggest($pspell_link, $list[$i]);
            foreach ($suggestions as $suggestion)
            {
                if(strlen($suggestion) > 1)
                {
                    $s = strtolower($suggestion);
                    $this->d($s,$a);
                    $this->e($s,$b);
                      
                    if($this->c($s,$a) == true && ctype_alpha($s))
                    {
                        $z[] = $s;
                    }
                }
                    
            }        
        }
        return $z;
    }
}
?>

New input page is simplified:

<?php
include 'perm.class.php';

# page outputs

if(empty($_POST['yl']))
{
    echo "<form action='n.php' method='POST'>
    Your Letters: <input type='text' name='yl' value='oor'>
    Board Letters: <input type='text' name='bl' value='dt'>
    <input type='submit'></form>";
}
else
{
    $a = $_POST['yl'];
    $b = $_POST['bl'];
    $play = new Perm();
    echo '<pre>';
    print_r($play->start($a,$b));
    echo '</pre>';
}
?>

Now: what is not working for you, explain me please.

Member Avatar
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Wait, one problem at a time.
An update on the class:

<?php
Class Perm
{

    public function start($arrayA,$arrayB)
    {
        global $a;
        global $b;
        
        $replace = array(',',' ','','-');
        $a = str_split(str_replace($replace,'',$arrayA));
        
        if(empty($arrayB))
        {
            $bArr = false;
            $b = array();
        } else {
            $bArr = true;
            $b = str_split(str_replace($replace,'',$arrayB));
        }
        
        $c = ($bArr == false) ? $a : array_merge($a,$b);
        
        if(count($c) > 5)
        {
            return 'too long';
        }
        else
        {
            $size = count($c)-1;
            $perm = range(0, $size);
            $j = 0;

            do {
                 foreach ($perm as $i) { $perms[$j][] = $c[$i]; }
            } while ($perm = $this->p($perm, $size) and ++$j);

            $list = array();
            foreach ($perms as $p) {
                $list[] = join('', $p);
            }
            
            return array_unique($this->ps($list));
        }
    }
    
    # generate permutations
    private function p($p, $size) {
        for ($i = $size-1; @$p[$i] >= $p[$i+1]; --$i) { }
        if ($i == -1) { return false; }
        for ($j = $size; $p[$j] <= $p[$i]; --$j) { }

        $tmp = $p[$i];
        $p[$i] = $p[$j];
        $p[$j] = $tmp;

        for (++$i, $j = $size; $i < $j; ++$i, --$j) {
             $tmp = $p[$i];
             $p[$i] = $p[$j];
             $p[$j] = $tmp;
        }

        return $p;
    }

    # count and compare values between array a and word 
    private function d($word, $array)
    {
	    global $d;
	    $u = array_unique($array);
	    $w = str_split($word);
     
	    $u2 = array_intersect($u,$w);
	    $a1 = array_count_values($array);
	    $w1 = array_count_values($w);
	    $a = array();
	    foreach($u2 as $k => $v)
	    {
        	$a[] = ($a1[$v] >= $w1[$v]) ? true:false;
	    }
     
	    $d = in_array(false,$a) ? false:true;
	    return $d;
    }
     
    # check array b
    private function e($word,$barray)
    {    
	    global $e;
	    $r = array();
	    $n = str_split($word);
	    $bn = 0;
	    if(count($barray) != 0 && is_array($barray))
	    {
		    for($i = 0; $i < count($n); $i++)
		    {
			    if(in_array($n[$i],$barray))
			    {
				    if($bn == 0)
				    {
					    $bn++;
					    $r[] = 1;
				    }
				    else
				    {
					    $bn++;
					    $r[] = 2;
				    }
			    }
			    else
			    {
				    $r[] = 3;
			    }
		    }
	    }
	    elseif(count($barray) == 0 && is_array($barray))
	    {
	        $r[] = 1;
	    }
	    else
	    {
	        $r[] = 0;
	    }
     
	    # codes:
	    # 0 no array
	    # 1 array yes, letter matched or empty array
	    # 2 array yes, more than one letter matched
	    # 3 array yes, no letter
     
	    $e = (in_array(1,$r) == true && in_array(2,$r) == false) ? true:false;
	    return $e;
    }

    # check array a
    private function c($word,$array)
    {
	    global $d;
	    global $e;
	    global $result;
	    $n = str_split($word);
	    $r = array();
	    for($i = 0; $i < count($n); $i++)
	    {
		    if(in_array($n[$i],$array))
		    {
			    $r[] = true;
		    }
		    elseif($e == true)
		    {
		        $r[] = true;
		    }
		    else
		    {
			    $r[] = false;
		    }		
	    }
     
	    $res = array();
	    $res[] = $d;
	    $res[] = $e;
	    $res[] = in_array(false,$r) ? false:true;
	    $result = in_array(false,$res) ? false:true;
	    return $result;
    }
    
    # check real words
    private function ps($list)
    {
        global $a;
        global $b;
        $n = count($list);
        $pspell_link = pspell_new("en"); # set language
        $z = array();
        for($i = 0; $i < $n; $i++)
        {
            $suggestions = pspell_suggest($pspell_link, $list[$i]);
            foreach ($suggestions as $suggestion)
            {
                if(strlen($suggestion) > 1)
                {
                    $s = strtolower($suggestion);
                    $this->d($s,$a);
                    $this->e($s,$b);
                      
                    if($this->c($s,$a) == true && ctype_alpha($s))
                    {
                        $z[] = $s;
                    }
                }
                    
            }        
        }
        return $z;
    }
}
?>

New input page is simplified:

<?php
include 'perm.class.php';

# page outputs

if(empty($_POST['yl']))
{
    echo "<form action='n.php' method='POST'>
    Your Letters: <input type='text' name='yl' value='oor'>
    Board Letters: <input type='text' name='bl' value='dt'>
    <input type='submit'></form>";
}
else
{
    $a = $_POST['yl'];
    $b = $_POST['bl'];
    $play = new Perm();
    echo '<pre>';
    print_r($play->start($a,$b));
    echo '</pre>';
}
?>

Now: what is not working for you, explain me please.

Thanks for all of your help. I still have letters that were not included in the text boxes though. Thanks!

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Update class function ps() with this:

private function ps($list)
    {
        global $a;
        global $b;
        $n = count($list);
        $pspell_config = pspell_config_create("en");
        
        # Available flags:
        #
        # PSPELL_FAST
        # PSPELL_NORMAL
        # PSPELL_BAD_SPELLERS # this will slow down the script
        #
        pspell_config_mode($pspell_config, PSPELL_BAD_SPELLERS);
        $pspell_link = pspell_new_config($pspell_config);
        $z = array();
        for($i = 0; $i < $n; $i++)
        {
            $suggestions = pspell_suggest($pspell_link, $list[$i]);
            foreach ($suggestions as $suggestion)
            {
                if(strlen($suggestion) > 1)
                {
                    $s = strtolower($suggestion);
                    $this->d($s,$a);
                    $this->e($s,$b);
                      
                    if($this->c($s,$a) == true && ctype_alpha($s))
                    {
                        $z[] = $s;
                    }
                }
                    
            }        
        }
        return $z;
    }

You can increase results by setting flags in pspell config as explained here: http://www.php.net/manual/en/function.pspell-config-mode.php
If it still doesn't match, please provide an example. Bye :)

Member Avatar
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Update class function ps() with this:

private function ps($list)
    {
        global $a;
        global $b;
        $n = count($list);
        $pspell_config = pspell_config_create("en");
        
        # Available flags:
        #
        # PSPELL_FAST
        # PSPELL_NORMAL
        # PSPELL_BAD_SPELLERS # this will slow down the script
        #
        pspell_config_mode($pspell_config, PSPELL_BAD_SPELLERS);
        $pspell_link = pspell_new_config($pspell_config);
        $z = array();
        for($i = 0; $i < $n; $i++)
        {
            $suggestions = pspell_suggest($pspell_link, $list[$i]);
            foreach ($suggestions as $suggestion)
            {
                if(strlen($suggestion) > 1)
                {
                    $s = strtolower($suggestion);
                    $this->d($s,$a);
                    $this->e($s,$b);
                      
                    if($this->c($s,$a) == true && ctype_alpha($s))
                    {
                        $z[] = $s;
                    }
                }
                    
            }        
        }
        return $z;
    }

You can increase results by setting flags in pspell config as explained here: http://www.php.net/manual/en/function.pspell-config-mode.php
If it still doesn't match, please provide an example. Bye :)

It still didn't work...

I entered in "rd" and "e" and got these results...

It shouldn't contain letters that weren't entered.

[0] => red
[1] => ride
[3] => rode
[4] => rude
[5] => rte
[7] => redo
[8] => rued
[9] => rodie
[10] => rudie
[11] => rodeo
[12] => rate
[13] => rite
[14] => rote
[15] => dre
[16] => de
[18] => re
[20] => rae
[21] => roe
[22] => rue
[23] => ade
[24] => ode
[26] => rye
[27] => wried
[28] => reit
[29] => read
[30] => reid
[31] => reta
[32] => rhodie
[34] => roadie
[35] => rheta
[36] => rhett
[37] => ready
[38] => route
[39] => write
[40] => wrote
[41] => drew
[43] => fred
[44] => bred
[45] => cred
[46] => ired
[47] => reds

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

So if you set rde you want to get only:

Array
(
    [0] => red
    [1] => dre
    [2] => de
    [4] => re
    [9] => ed
    [12] => der
    [21] => er
)

Am I right? If yes maybe we got it, now there is a function at the end, that will check only for words containing set letters:

<?php
Class Perm
{
    
    public function start($arrayA,$arrayB)
    {
        global $a;
        global $b;
        global $c;
        
        $replace = array(',',' ','','-');
        $a = str_split(str_replace($replace,'',$arrayA));
        
        if(empty($arrayB))
        {
            $bArr = false;
            $b = array();
        } else {
            $bArr = true;
            $b = str_split(str_replace($replace,'',$arrayB));
        }
        
        $c = ($bArr == false) ? $a : array_merge($a,$b);
        
        if(count($c) > 5)
        {
            return 'too long';
        }
        elseif(count($c) <= 1)
        {
            return 'too short';
        }
        else
        {
            $size = count($c)-1;
            $perm = range(0, $size);
            $j = 0;

            do {
                 foreach ($perm as $i) { $perms[$j][] = $c[$i]; }
            } while ($perm = $this->p($perm, $size) and ++$j);

            $list = array();
            foreach ($perms as $p) {
                $list[] = join('', $p);
            }
            
            return array_unique($this->ps($list));
        }
    }
    
    # generate permutations
    private function p($p, $size) {
        for ($i = $size-1; @$p[$i] >= $p[$i+1]; --$i) { }
        if ($i == -1) { return false; }
        for ($j = $size; $p[$j] <= $p[$i]; --$j) { }

        $tmp = $p[$i];
        $p[$i] = $p[$j];
        $p[$j] = $tmp;

        for (++$i, $j = $size; $i < $j; ++$i, --$j) {
             $tmp = $p[$i];
             $p[$i] = $p[$j];
             $p[$j] = $tmp;
        }

        return $p;
    }

    # count and compare values between array a and word 
    private function d($word, $array)
    {
	    global $d;
	    $u = array_unique($array);
	    $w = str_split($word);
     
	    $u2 = array_intersect($u,$w);
	    $a1 = array_count_values($array);
	    $w1 = array_count_values($w);
	    $a = array();
	    foreach($u2 as $k => $v)
	    {
        	$a[] = ($a1[$v] >= $w1[$v]) ? true:false;
	    }
     
	    $d = in_array(false,$a) ? false:true;
	    return $d;
    }
     
    # check array b
    private function e($word,$barray)
    {    
	    global $e;
	    $r = array();
	    $n = str_split($word);
	    $bn = 0;
	    if(count($barray) != 0 && is_array($barray))
	    {
		    for($i = 0; $i < count($n); $i++)
		    {
			    if(in_array($n[$i],$barray))
			    {
				    if($bn == 0)
				    {
					    $bn++;
					    $r[] = 1;
				    }
				    else
				    {
					    $bn++;
					    $r[] = 2;
				    }
			    }
			    else
			    {
				    $r[] = 3;
			    }
		    }
	    }
	    elseif(count($barray) == 0 && is_array($barray))
	    {
	        $r[] = 1;
	    }
	    else
	    {
	        $r[] = 0;
	    }
     
	    # codes:
	    # 0 no array
	    # 1 array yes, letter matched or empty array
	    # 2 array yes, more than one letter matched
	    # 3 array yes, no letter
     
	    $e = (in_array(1,$r) == true && in_array(2,$r) == false) ? true:false;
	    return $e;
    }

    # check array a
    private function c($word,$array)
    {
	    global $d;
	    global $e;
	    global $result;
	    $n = str_split($word);
	    $r = array();
	    $from = array();
	    for($i = 0; $i < count($n); $i++)
	    {
		    if(in_array($n[$i],$array))
		    {
			    $r[] = true;
		    }
		    elseif($e == true)
		    {
		        $r[] = true;
		    }
		    else
		    {
			    $r[] = false;
		    }		
	    }
     
	    $res = array();
	    $res[] = $d;
	    $res[] = $e;
            $res[] = in_array(false,$r) ? false:true;
	    $result = in_array(false,$res) ? false:true;
	    return $result;
    }
    
    # get suggestions
    private function ps($list)
    {
        global $a;
        global $b;
        $n = count($list);
        $pspell_config = pspell_config_create("en");
        
        # Available modes:
        #
        # PSPELL_FAST
        # PSPELL_NORMAL
        # PSPELL_BAD_SPELLERS # this will slow down the script
        #
        pspell_config_mode($pspell_config, PSPELL_FAST);
        $pspell_link = pspell_new_config($pspell_config);
        $z = array();
        for($i = 0; $i < $n; $i++)
        {
            $suggestions = pspell_suggest($pspell_link, $list[$i]);
            foreach ($suggestions as $suggestion)
            {
                if(strlen($suggestion) > 1)
                {
                    $s = strtolower($suggestion);
                    $this->d($s,$a);
                    $this->e($s,$b);
                      
                    if($this->c($s,$a) == true && ctype_alpha($s) && $this->check($s) == true)
                    {
                        $z[] = $s;
                    }
                }
                    
            }
        }
        return $z;
    }
    
    # filter
    private function check($word)
    {
        global $c;
        $w = str_split($word);
        $chklst = array();
        for($i = 0; $i < count($w); $i++)
        {
            $chklst[] = (in_array($w[$i],$c)) ? true:false;
        }
        return in_array(false,$chklst) ? false:true;
    }



}
?>
Attachments n.php (0.42KB) perm.class_.php (4.96KB)
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: