2016-07-19 7 views
7

Próbuję znaleźć najlepszy sposób zamiany zastępowania ciągu z wieloma sortowaniami.Zamiennik ciągu Foreach z wieloma odmianami

Mam zdanie, które jest wstawiane przez użytkownika, mam tablicę, która zawiera wszystkie słowa w tym słowie i ich potencjalne poprawki.

$sentence = 'i want to recovary my vehical from the cabs';

chcę, aby wyświetlić następujące:

  1. chcę odzyskania mojego pojazdu z kabiną
  2. chcę odzyskać moje pojazdu z kabiną
  3. chcę revary mój pojazd z kabiny

Kod do tej pory:

$element = array(
    "vehical" => array('vehicle'), 
    "recovary" => array('recovery', 'recover', 'revary') 
); 

$sentence = 'i want to recovary my vehical from the cabs'; 

foreach($element as $i => $val){ 
    echo $i;  
} 

EDIT: Rozszerzone inny scenariusz:

Co by się stało, gdyby nie było więcej niż jeden wariant w górnej tablicy.

"vehical" => array('vehicle', 'vehiclesy', 'whatever'), 
    "recovary" => array('recovery', 'recover', 'revary') 
  1. chcę odzyskania mojego pojazdu z kabiną
  2. chcę odzysku mój vehiclesy od kabin
  3. chcę odzysku moje cokolwiek z kabin
  4. Chcę odzyskać swoje pojazd z kabiny
  5. Chcę odzyskać moje pojazdy z kabin
  6. Chcę odzyskać moje wszystko z kabiny
  7. chcę revary mojego pojazdu z kabiną
  8. chcę revary mój vehiclesy z kabin
  9. Chcę revary moje cokolwiek z kabin

Odpowiedz

2

Należy utworzyć wszystkie unikalne kombinacje danych zastępczych .Dla każdej z tych kombinacji możesz dokonać wymiany. Jest to jeden ze sposobów, aby to zrobić:

<?php 
function createCombinations(array $input) 
{ 
    $head = array_shift($input); 
    $tail = count($input) > 1 ? createCombinations($input) : array_shift($input); 

    $combinations = []; 
    foreach ($head as $left) { 
     foreach ($tail as $right) { 
      $combinations[] = array_merge([$left], (array) $right); 
     } 
    } 

    return $combinations; 
} 

$element = [ 
    'vehical' => ['vehicle', 'car'], 
    'recovary' => ['recovery', 'recover', 'revary'], 
    'cubs'  => ['cabs'], 
]; 

$sentence = 'i want to recovary my vehical from the cubs'; 
$from = array_keys($element); 

foreach (createCombinations($element) as $to) { 
    echo str_replace($from, $to, $sentence), "\n"; 
} 

# => i want to recovery my vehicle from the cabs 
# => i want to recover my vehicle from the cabs 
# => i want to revary my vehicle from the cabs 
# => i want to recovery my car from the cabs 
# => i want to recover my car from the cabs 
# => i want to revary my car from the cabs 

demo. https://ideone.com/LERb9X

2

spróbuje użyć str_replace() podobnego

$str=''; 
foreach($element as $search => $combinations){ 
    foreach($combinations as $comb){ 
     $str.=str_replace($search,$comb,$sentence)."\n"; 
    } 
} 
echo $str; 

Demo

Postaraj się, aby funkcje i stworzyć tablicę wszystkich możliwych kombinacji ponownie umieścić go przy użyciu powstałego szereg podobnych

$element = array(
    "vehical" => array('vehicle', 'vehiclesy', 'whatever'), 
    "recovary" => array('recovery', 'recover', 'revary') 
); 

$sentence = 'i want to recovary my vehical from the cabs'; 

// change the array using loop for replacement 
function makeCombinations($combinations, $values) 
{ 
    $res = array(); 
    $i=0; 
    foreach($combinations as $comb) { 
     foreach($values as $value) { 
      $res[$i] = is_array($comb) ? $comb : array($comb); 
      $res[$i][] = $value; 
      $i++; 
     } 
    } 
    return $res; 
} 

$searchArr = array(); 
foreach($element as $search => $values) { 
    $searchArr[] = $search; 
    $combinations = isset($combinations) ? makeCombinations($combinations, $values) : $values; 
} 

// finally replace the strings 
foreach($combinations as $combination){ 
    echo str_replace($searchArr, $combination, $sentence),"\n"; 
} 

Demo

+0

właśnie rozszerzył pytanie - Przeprosiny :( –

+0

@SophieRhodes próbują moją odpowiedź, że działa zgodnie z oczekiwaniami –

2

Spróbuj następujące rozwiązanie, przy użyciu sekund (foreach i str_replace())

//the items with replacement values. 
$items = array(
    "vehical" => array('vehicle', 'vehiclesy', 'whatever'), 
    "recovary" => array('recovery', 'recover', 'revary'), 
    "cabs" => array('cups', 'cips'), 
    "from" => array(1, 2) 
); 

//init the initial sentence and the array with all solutions. 
$sentence = 'i want to recovary my vehical from the cabs'; 
$solution = []; 

//run through all keywords to execute the replacements. 
foreach ($items as $item => $value) { 
    if (count($value) > 0) { 
     if (count($solution) > 0) { 
      $solution = getReplacements($solution, $item, $value); 
     } else { 
      $solution = getReplacements($sentence, $item, $value); 
     } 
    } 
} 

//output the solutions. 
array_walk_recursive($solution, 'output'); 

function output(&$item,$key) { 
    echo $item."\n"; 
} 

/** 
* Function to execute the replacements. 
* @param array|string $sentence An array or string on which the replacements should execute. 
* @param string $item The word which will be replaced. 
* @param array $values The replacement values for the item. 
* @return array An array with all solutions of this function. 
*/ 
function getReplacements($sentence, $item, $values) 
{ 
    $solutions = []; 

    foreach ($values as $value) { 
     $sol = str_replace($item, $value, $sentence); 

     if (is_array($sol)) { 
      $solutions = array_merge($solutions, $sol); 
     } else { 
      $solutions[] = $sol; 
     } 
    } 

    return $solutions; 
} 

Demo:https://ideone.com/X2Pg1R

+0

właśnie rozszerzył pytanie - przeprosiny: ( –

+0

zaktualizowałem moją odpowiedź ... - działa teraz z obu tablic i bez złych zdań. –