2016-07-19 10 views
7

我想了解使用多個歸類的字符串替換的最佳方法。多個變體的foreach字符串替換

我有一個句子是由用戶插入的,我有一個數組,其中包含所有拼寫錯誤的單詞以及它們的潛在更正。

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

我想顯示如下:

  1. 我想恢復我的車從駕駛室
  2. 我想從駕駛室恢復我的車
  3. 我想revary我的車從出租車

到目前爲止的代碼:

$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;  
} 

編輯:擴展的另外一種情況:

如果有頂陣列中的一個以上的變化,會發生什麼。

"vehical" => array('vehicle', 'vehiclesy', 'whatever'), 
    "recovary" => array('recovery', 'recover', 'revary') 
  1. 我想恢復從駕駛室我的車
  2. 我想恢復從駕駛室我vehiclesy
  3. 我想恢復我不管從駕駛室
  4. 我想恢復我車輛從出租車
  5. 我想從出租車恢復我的車輛
  6. 我想從出租車恢復我的任何東西
  7. 我想從駕駛室revary我的車
  8. 我想從駕駛室
  9. 我想revary revary我vehiclesy我無論從出租車

回答

2

您需要創建替換數據的所有唯一組合。對於這些組合中的每一種,您都可以進行更換。這是做這件事:

<?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 

演示:https://ideone.com/LERb9X

2

嘗試使用str_replace()一樣,

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

Demo

儘量使功能和創建的所有可能組合的陣列,然後重新將其放置使用所得的陣列等,

$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

剛剛擴展的問題 - 道歉:( –

+0

@SophieRhodes試試我的答案,它正在爲你的預期 –

2

嘗試下面的解決方案,使用一個秒(的foreach和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; 
} 

演示:https://ideone.com/X2Pg1R

+0

剛剛擴展的問題 - 道歉: ( –

+0

更新了我的答案... - 現在正在使用這兩個數組並且沒有錯誤的句子。 –