【问题标题】:PHP String Differences and Dynamic RestrictionsPHP 字符串差异和动态限制
【发布时间】:2013-02-28 17:32:07
【问题描述】:

示例A【简化版】:------------------------------------ --------------------------------

MODEL:字符串 {1} 并保持在 {2}和oooon...

CASE_A:字符串 hello 并保持 两个单词 和 oooon。 ..

CASE_B:字符串其他任何东西并继续只是为了好玩和呜呜呜……

我需要得到一个列表,其中包含 n 个名为 $v1$v2$vn... 的变量及其各自的匹配值:

编辑
请注意,变量名称取决于占位符。占位符始终是 INT。 (这些数字只是索引而不是字数)

对于案例 A
$v1=你好
$v2=两个字
$vn=等...

对于案例 B
$v1=其他任何事情
$v2=只是为了好玩
$vn =...

如您所见,获取这些值的引用是两个字符串的“constant”部分。

示例 B [几乎是真实的]: ---------------------------------- ----------------------------------

现在我们假设每个可能的匹配都保存在一个数组中(实际情况是一个长数据库),像这样:

可能的匹配数{
[0] 字符串三 [1] 单字 [2] 其他东西
[3] 哈利·波波特
[4] 两个字 [5] 神奇的文字神奇的感觉
}

在前面的示例中没有必要,因为每个 {n}placeholder” 都由“constant” 字符串分隔。但是在某些情况下,这些“占位符”是在一起的……所以我必须发明一种新的方法来匹配可能的匹配(固定列表)。

字符串 {1} 并保持在 {2} {3} 和 oooon...

字符串 一个单词 并保持 两个单词 字符串三个 和 oooon...

如您所见(基于上面显示的数组),结果应该是:
$v1=hello
$v2= 两个字
$v3=字符串三

但是 PHP 怎么知道我希望我的字符串如何分开

我的想法正在做下一个:

1) 将 {2}{3} 块作为单个
2)如果这个块(两个词和三个词)in_array()
3) 如果不是:
4) 去掉它的最后一个字
5)用新的(两个词和三个)再次检查
6) 如果不是:
4') 删除它的最后一个单词
5') 再次检查新的(两个词和)
4'') 去掉最后一个字
5'') 再次检查新的(两个单词)
7) 重复 4 和 5 直到它是一个 可能的匹配 (in_array())
8) 匹配的将是 {2},字符串的其余部分将是 {3}

我的问题 : 我怎样才能在 PHP 中做到这一点?
我试图以最简单的方式解释它,我希望你能理解我想要问什么。如果有人需要更多示例,我会将它们写下来,请告诉我。感谢阅读。

编辑 ------------------------------------ --------------------------------
一个真实的例子:

数组:possible_matches{
[0] 克里斯托弗·约翰逊
[1] McCandless
[2] 电影院
[3] 明天晚上
}

MODEL:我的名字是 {1} {2},我要去 {3}{4}

案例:我的名字是 Christopher Johnson McCandless,我是明天晚上去电影院

期望的结果:
$v1=Christopher Johnson
$v2=McCandless
$v3 =电影院
$v4=明天晚上

创建可能的组合数组

function get_possible_groups($string_of_words, $groups_count){
$words=explode(' ',$string_of_words);
$total=count($words);
$group_1=array(array());
$group_2=array(array());
//We can create TOTAL-1 combinations
for($i=0;$i<$total;$i++){
$lim=$total-$i-1;
    for($j=0;$j<$total;$j++){
        if($j<$lim){
            $group_1[$i][]=$words[$j];
        }else{
            $group_2[$i][]=$words[$j];
        }
    }
}
return array($group_1,$group_2);
}

在 cmets 中引用了 ACCEPTED 答案的更新

$model="Damn you {1}, {2} will kill you. {3}{4}{5}";
//Array => Save how many single placeholders are in each "placeholder block"
$placeholder_count{
[0]=1, //first block contains one placeholder
[1]=1, //second block contains one placeholder
[2]=3  //third block contains three placeholders
}
//Simplify all blocks into ONE SINGLE regex placeholder
$pattern="/Damn you (.*), (.*) will kill you. (.*)/";

//Match in string
$string="Damn you Spar, Will will kill you. I Love it man.";
preg_match($pattern,$string,$matches);

//View in array which placeholders have to be checked
$block_0=$matches[1]; //Array shows it was 1 p.holder. No check needed
$block_1=$matches[2]; //Array shows it was 1 p.holder. No check needed
$block_2=$matches[3]; //It shows it were 3 p.holders. Possible grouping (n=3)

//Result
$v1=$matches[1];
$v2=$matches[2];

$v3,$v4,$v5=(Result of grouping and querying the $matches[3] with groups_count=3)

【问题讨论】:

  • 你怎么知道要找什么?
  • 好的,{1}, {2} 是占位符,可以包含占位符中所说的确切字数。对于给定的随机字符串,需要处理多少个这样的模型?
  • 好的,数字只是索引而不是字数。你能举出不是你自己的实际例子吗?
  • 这样更好,possible_matches 数组可以有两个以上的元素,对吧?为什么and three words 被丢弃了?因为and three words 不在possible_matches 数组中?
  • 哦,你删除了那些部分。这意味着所有单词都很重要,而且没有一个可以被丢弃,对吧?

标签: php mysql match


【解决方案1】:

Christopher Johnson McCandless 映射到{1}{2} 时:

形成两组的可能组合是:

  • Christopher JohnsonMcCandless
  • ChristopherJohnson McCandless

cinema tomorrow at night 映射到{3}{4}

形成两组的可能组合是:

  • cinematomorrow at night
  • cinema tomorrowat night
  • cinema tomorrow atnight

写一个 PHP 函数 get_possible_groups($string_of_words, $group_count) 返回组组合数组。

以及如下 SQL 语句:

SELECT count(*), 'cinema' firstWordGroup, 'tomorrow at night' secondWordGroup
  FROM possibleMatchTable
 WHERE possible_match IN ('cinema', 'tomorrow at night')
UNION
SELECT count(*), 'cinema tomorrow', 'at night'
  FROM possibleMatchTable
 WHERE possible_match IN ('cinema tomorrow', 'at night')
UNION
SELECT count(*), 'cinema tomorrow at', 'night'
  FROM possibleMatchTable
 WHERE possible_match IN ('cinema tomorrow at', 'night');

一个可能的输出可以是:

+----------+--------------------+-------------------+
| count(*) | firstWordGroup     | secondWordGroup   |
+----------+--------------------+-------------------+
|        2 | cinema             | tomorrow at night |
|        0 | cinema tomorrow    | at night          |
|        0 | cinema tomorrow at | night             |
+----------+--------------------+-------------------+

以计数为 2(两个词组)的为准。

如果 MODEL 文本是 fulltext 索引列,那么对于任何给定的随机字符串,您可以获得最相关的模型,例如:

SELECT * FROM model_strings 
WHERE MATCH(model) AGAINST ('Damn you Spar, Kot will kill you.');

查询可能会返回如下内容:

+----------------------------------+
| model                            |
+----------------------------------+
| Damn you {1}, {2} will kill you. |
+----------------------------------+

使用Model中的占位符提取随机字符串的单词:

<?php 

$placeholder_pRegEx = '#\{\d+\}#';

$model = 'Damn you {1}, {2} will kill you. {3}{4}{5}';
$string = 'Damn you Spar, Will will kill you. I Love it man.';

$model_words = explode(' ', $model);
$string_words = explode(' ', $string);

$placeholder_words = array();

for ($idx =0, $jdx=0; $idx < count($string_words); $idx ++) {

    if ($jdx < count($model_words)) {
        if (strcmp($string_words[$idx], $model_words[$jdx])) {
            $placeholder_words[] = $string_words[$idx];

            //Move to next word in Model only if it's a placeholder
            if (preg_match($placeholder_pRegEx, $model_words[$jdx]))
                $jdx++;

        } else
            $jdx++; //they match so move to next word
    } else
        $placeholder_words[] = $string_words[$idx];
}

//Even status will have the count
$status = preg_match_all ($placeholder_pRegEx, $model, $placeholders);

$group_count = count($placeholders[0]);

var_dump(get_defined_vars());
?>

以上代码将为您提供如下值:

'placeholder_words' => array (size=6)
  0 => string 'Spar,' (length=5)
  1 => string 'Will' (length=4)
  2 => string 'I' (length=1)
  3 => string 'Love' (length=4)
  4 => string 'it' (length=2)
  5 => string 'man.' (length=4)

'placeholders' => array (size=1)
  0 => 
    array (size=5)
      0 => string '{1}' (length=3)
      1 => string '{2}' (length=3)
      2 => string '{3}' (length=3)
      3 => string '{4}' (length=3)
      4 => string '{5}' (length=3)

'group_count' => int 5
  • 您可以从那里致电get possible groupings
  • 然后使用 SQL 查询检查允许的可能匹配项
  • 所需分组中的实际单词。

唉,这是个问题,嗯!

【讨论】:

  • 得到你的功能;使其通用,它应该返回数组数组。
  • 将 3 个或更多占位符放在一起会更难。说像{7}{8}{9}{10}..{n} 并排在一起。如果你有那个通用的 php get_possible_groups() 就很容易。希望您已在此 possible_matches 列上建立索引。
  • 您能否在您的问题中添加该功能。或者您也可以使用该功能发布答案。大多数时候,审稿人都会拒绝你对我帖子的修改。
  • Coz,如果你有这个功能,可能的组合已经准备好 => 查询已经准备好 => 用单个 SQL 查询直接回答。
  • 如果placeholder 中有多个单词,那么它就是multiplaceholder 的候选对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-17
  • 2018-02-10
  • 2010-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多