Need help regex, .... Word Possiblity ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
putu_niki
Forum Newbie
Posts: 2
Joined: Tue Sep 20, 2005 9:08 pm
Location: Bali

Need help regex, .... Word Possiblity ?

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
putu_niki
Forum Newbie
Posts: 2
Joined: Tue Sep 20, 2005 9:08 pm
Location: Bali

Thank i will try that. Ro feyd

Post by putu_niki »

Thank feyd,

I Will try that.

Would you like to tell me your mail.

Thank
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: Thank i will try that. Ro feyd

Post 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
Post Reply