首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多变量前置字符串替换

多变量前置字符串替换
EN

Stack Overflow用户
提问于 2016-07-19 12:06:47
回答 3查看 310关注 0票数 8

我试图找出用多个排序规则替换字符串的最佳方法。

我有一个由用户插入的句子,我有一个数组,其中所有拼写错误的单词及其潜在的更正。

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

我想显示以下内容:

  1. 我想把我的车从计程车里找回来
  2. 我想把我的车从计程车里找回来
  3. 我想从计程车上修理我的车

目前为止的代码:

代码语言:javascript
复制
$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;    
}

编辑:扩展另一个场景:

如果顶部数组中有一个以上的变化会发生什么。

代码语言:javascript
复制
    "vehical" => array('vehicle', 'vehiclesy', 'whatever'),
    "recovary" => array('recovery', 'recover', 'revary')
  1. 我想把我的车从计程车里找回来
  2. 我想把我的车从计程车里弄回来
  3. 我想从计程车里找回我的一切
  4. 我想把我的车从计程车里找回来
  5. 我想把我的车从计程车里找回来
  6. 我想从计程车里找回我的东西
  7. 我想从计程车上修理我的车
  8. 我想从计程车里修理我的车
  9. 我想从计程车里拿出我的任何东西
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-07-19 13:08:10

您需要创建替换数据的所有唯一组合。对于这些组合中的每一个,您都可以进行替换。这是一种方法:

代码语言:javascript
复制
<?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
EN

Stack Overflow用户

发布于 2016-07-19 12:14:25

试着使用替换()

代码语言:javascript
复制
$str='';
foreach($element as $search => $combinations){
     foreach($combinations as $comb){
        $str.=str_replace($search,$comb,$sentence)."\n";
     }
}
echo $str;

演示

尝试创建函数并创建一个包含所有可能组合的数组,然后使用结果数组替换它,

代码语言:javascript
复制
$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";
}

演示

票数 2
EN

Stack Overflow用户

发布于 2016-07-19 12:16:28

使用秒(foreach和str_replace())尝试以下解决方案:

代码语言:javascript
复制
//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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38458088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档