Page 1 of 1

Need help regex, .... Word Possiblity ?

Posted: Tue Sep 20, 2005 9:20 pm
by putu_niki
Dear friend's

Word Possiblity,
Would you like to help me. I have a problem with regex.
How can i match the word :
The input is bank word and character, then will find from

that character how word find in bank word input. Please post
me the code.

Example :

Input bank words
ant
bee
cat
dog
ewe
fly
gnu

Input character:
b e w
b b e e w w
t a n c u g d

Output:
0
2
3

Input charackter check with input bank word if match then

show output (how much match), if not output 0. Thank for

your help.

Regards,
Putu

Posted: Tue Sep 20, 2005 10:42 pm
by feyd
I'm going to assume that each character can only be used 1 time during a word.... in that case.. regex will not help much..here's an idea to head down however...

Code: Select all

<?php

$words = array('ant','bee','cat','dog','ewe','fly','gnu');
$letters = array('bbw','bbeeww','tancugd');

$wordsCopy = preg_replace('#[\W]+#s','',$words);
foreach($wordsCopy as $key => $word)
{
  $wordsCopy[$key] = preg_split('#(.)#s',strtolower($word),-1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  sort($wordsCopy[$key]);
}

$letters = preg_replace('#[\W]+#s','',$letters);
foreach($letters as $key => $letter)
{
  $letters[$key] = preg_split('#(.)#s',strtolower($letter),-1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  sort($letters[$key]);
}

$counts = array();
foreach($letters as $letter)
{
  $ls = implode('',$letter);
  $counts[$ls] = 0;
  foreach($wordsCopy as $word)
  {
    $oldword = implode('',$word);
    foreach($letter as $l)
    {
      $pos = array_search($l,$word);
      if($pos !== false)
      {
        array_splice($word,$pos,1);
      }
    }
    if(count($word) == 0)
    {
      $counts[$ls]++;
    }
  }
}

var_export($counts);

?>
outputs

Code: Select all

array (
  'bbw' => 0,
  'bbeeww' => 2,
  'acdgntu' => 3,
)

FYI, cross-posting is bad, mmmmkay?

Thank i will try that. Ro feyd

Posted: Fri Sep 23, 2005 11:14 am
by putu_niki
Thank feyd,

I Will try that.

Would you like to tell me your mail.

Thank

Re: Thank i will try that. Ro feyd

Posted: Fri Sep 23, 2005 12:12 pm
by feyd
putu_niki wrote:Would you like to tell me your mail.
I'm curious, why?


You can find it in my Code Snippet board threads: viewtopic.php?t=32334