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
Need help regex, .... Word Possiblity ?
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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...outputs
FYI, cross-posting is bad, mmmmkay?
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);
?>Code: Select all
array (
'bbw' => 0,
'bbeeww' => 2,
'acdgntu' => 3,
)FYI, cross-posting is bad, mmmmkay?
Thank i will try that. Ro feyd
Thank feyd,
I Will try that.
Would you like to tell me your mail.
Thank
I Will try that.
Would you like to tell me your mail.
Thank
- 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
I'm curious, why?putu_niki wrote:Would you like to tell me your mail.
You can find it in my Code Snippet board threads: viewtopic.php?t=32334