Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreach string replacement with multiple variations

Tags:

arrays

loops

php

I am trying to find out the best way of going about string replacement with multiple collations.

I have a sentence which is inserted by user, i have an array which of all the miss-spelled words in that sentence and their potential corrections.

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

I want to display the following:

  1. i want to recovery my vehicle from the cabs
  2. i want to recover my vehicle from the cabs
  3. i want to revary my vehicle from the cabs

Code so far:

$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: Expanded another scenario:

What would happen if there was more than one variation in the top array.

    "vehical" => array('vehicle', 'vehiclesy', 'whatever'),
    "recovary" => array('recovery', 'recover', 'revary')
  1. i want to recovery my vehicle from the cabs
  2. i want to recovery my vehiclesy from the cabs
  3. i want to recovery my whatever from the cabs
  4. i want to recover my vehicle from the cabs
  5. i want to recover my vehiclesy from the cabs
  6. i want to recover my whatever from the cabs
  7. i want to revary my vehicle from the cabs
  8. i want to revary my vehiclesy from the cabs
  9. i want to revary my whatever from the cabs
like image 626
Sophie Rhodes Avatar asked Jul 19 '16 12:07

Sophie Rhodes


2 Answers

You need to create all unique combinations of replacement data. For each of those combinations you can then do the replacement. This is one way to do it:

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

like image 63
Yoshi Avatar answered Nov 11 '22 23:11

Yoshi


Try to use str_replace() like,

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

Demo

Try to make functions and create an array of all possible combinations then replace it using the resultant array like,

$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

like image 21
Rohan Kumar Avatar answered Nov 12 '22 01:11

Rohan Kumar