Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to splitting words match by patterns with PHP

Tags:

php

I have an array of words. e.g.,

$pattern = ['in', 'do', 'mie', 'indo'];

I wanna split a words match by the patterns to some ways.

input =

indomie

to output =

$ in, do, mie
$ indo, mie

any suggests?

*ps sorry for bad english. thank you very much!

like image 471
CreamerCode Avatar asked Mar 13 '26 06:03

CreamerCode


1 Answers

it was an very interesting question.

Input:-

$inputSting = "indomie";

$pattern = ['in', 'do', 'mie', 'indo','dom','ie','indomi','e'];

Output:-

in,do,mie

in,dom,ie

indo,mie

indomi,e

Approach to this challenge

  1. Get the pattern string length

  2. Get the all the possible combination of matrix

  3. Check whether the pattern matching.

if i understand your question correctly then above @V. Prince answer will work only for finding max two pattern.


    function sampling($chars, $size, $combinations = array()) {

        if (empty($combinations)) {
            $combinations = $chars;
        }

        if ($size == 1) {
            return $combinations;
        }

        $new_combinations = array();

        foreach ($combinations as $combination) {
            foreach ($chars as $char) {
                $new_combinations[] = $combination . $char;
            }
        }

        return sampling($chars, $size - 1, $new_combinations);
    }



    function splitbyPattern($inputSting, $pattern)
    {

        $patternLength= array();
        // Get the each pattern string Length
        foreach ($pattern as $length) {

            if (!in_array(strlen($length), $patternLength))
            {
                array_push($patternLength,strlen($length));
            }

        }
        // Get all the matrix combination of pattern string length to check the probablbe match
        $combination = sampling($patternLength, count($patternLength));
        $MatchOutput=Array();

        foreach ($combination as $comp) {
            $intlen=0;
            $MatchNotfound = true;
            $value="";

            // Loop Through the each probable combination
            foreach (str_split($comp,1) as $length) {
                 if($intlen<=strlen($inputSting))
                 {
                     // Check whether the pattern existing
                     if(in_array(substr($inputSting,$intlen,$length),$pattern))
                    {
                        $value = $value.substr($inputSting,$intlen,$length).',';
                    }
                    else
                    {
                        $MatchNotfound = false;
                        break;
                    }
                 }
                 else
                 {           
                    break;
                 }

                $intlen = $intlen+$length;
            }       
            if($MatchNotfound)
            {
                array_push($MatchOutput,substr($value,0,strlen($value)-1)); 
            }
        }

        return array_unique($MatchOutput);
    }

 $inputSting  = "indomie";

    $pattern = ['in', 'do', 'mie', 'indo','dom','ie','indomi','e'];

    $output = splitbyPattern($inputSting,$pattern);

    foreach($output  as $out)
    {
        echo $out."<br>";
    }

    ?>
like image 151
Anbazhagan IND Avatar answered Mar 14 '26 22:03

Anbazhagan IND