【问题标题】:Filter, search words in string过滤,搜索字符串中的单词
【发布时间】:2013-07-12 23:58:07
【问题描述】:

好的,我有以下数据:

liagel - ag, el, li, age, gel, lia

我正在尝试找到一种方法来获得字符串的完全匹配,您会看到字符串中存在所有单词,但我想以这种方式过滤它,所以我得到的最终结果是:

liagel = lia 凝胶

谢谢。

【问题讨论】:

  • 匹配总是分两段进行
  • 我认为字符串中的单词不会超过 3 个。另一个例子是 anarad - ad, an, ar, na, ana, rad ... 是的,结果可能不止 1 个。
  • 这个例子也匹配了 2 个元素
  • 但是有两个变体,两个词和三个词:an,ar,ad - ana,rad
  • 您是否希望程序同时返回两种变体

标签: php string filter


【解决方案1】:

解决方案:

我通过使用 PEAR http://pear.php.net/package/Math_Combinatorics/ 的数学组合库找到了一个快速简单的解决方案(该类创建了关键字的所有变体,然后我将这些变体与我的原始字符串进行匹配并获得最终结果)。简单的代码(来自 2 个单词)如下所示:

require_once 'library/Combinatorics.php';
$c = new Math_Combinatorics;

$words = array('ag', 'el', 'li', 'age', 'gel', 'lia');
$string = 'liagel';

foreach($c->permutations($words, 2) as $p) {

    $tmp_word = join('', $p);
    $tmp_word_2 = join(' ', $p);

    if ($tmp_word == $string) {

        $found[$string] = $tmp_word_2;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    相关资源
    最近更新 更多