PHP字符串差异和动态限制 [英] PHP String Differences and Dynamic Restrictions

查看:72
本文介绍了PHP字符串差异和动态限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例A [简化的示例]:------------------------------------ --------------------------------

模型:字符串 {1} 并在 {2} 上保留还有哦...

MODEL: String {1} and keeeeeep on {2} and oooon...

CASE_A :字符串 hello 并在 两个词 和oooon上使用keeeeeep. ..

CASE_A: String hello and keeeeeep on two words and oooon...

CASE_B :字符串 其他任何 ,并在 只是为了好玩 和keeeeeep呜...

CASE_B: String anything else and keeeeeep on just for fun and oooon...

我需要获取带有 n 个变量的列表,它们分别为$v1$v2$vn ...,并具有各自的匹配值:

编辑:
请注意,变量名是根据占位符给出的.占位符始终是INT. (数字只是索引而不是字数)

对于案例A :
$v1 = 你好
$v2 = 两个词
$vn = ...

对于情况B :
$v1 = 其他
$v2 = 只是为了好玩
$vn = ...

如您所见,获取这些值的引用是两个字符串的"恒定"部分.

I need to get a list with n vars called $v1, $v2, $vn... with their respective matched values:

EDIT:
Note that the variable names are given depending on the placeholder. Placeholders are always an INT. (The numbers are just indexes not word count)

For case A:
$v1=hello
$v2=two words
$vn=etc...

For case B:
$v1=anything else
$v2=just for fun
$vn=etc...

As you can see the references to get these values are the "constant" parts of both strings.

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

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

possible_matches {
[0] 字符串三<<<<
[1] 一个字<<<<
[2] 其他内容
[3] Harry Poppotter
[4] 两个字<<<<
[5] 魔术词魔术感觉
}

Now we suppose that each possible match is saved in an array (real case is a long database), like this:

possible_matches{
[0] string three <<<<
[1] Oneword <<<<
[2] Other stuff
[3] Harry Poppotter
[4] two words <<<<
[5] Magic words magic feelings
}

在前面的示例中,这不是必需的,因为每个 {n} "占位符"由"常量"字符串分隔.但是在某些情况下,这些"占位符"在一起……所以我必须发明一种新的方式来匹配可能的匹配(固定列表).

In the previous example it was not necessary, because each {n} "placeholder" was separated by "constant" strings. But there are cases where these "placeholders" are together... so I have to invent a new way to match the possible matches (fixed list).

{2} {3}上

String {1} 和keeeeeep 和oooon ...

String {1} and keeeeeep on {2} {3} and oooon...

字符串 一个单词 和keeeeeep在 两个单词 字符串三 和oooon ...

String Oneword and keeeeeep on two words string three and oooon...

您可以看到(基于上面显示的数组),结果应为:
$v1 = 你好
$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中做到这一点?
我试图以最简化的方式对其进行解释,希望您理解我要提出的要求.如果有人需要更多示例,我会写下来,请告诉我.感谢您的阅读.

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

数组:可能的匹配项 {
[0] 克里斯托弗·约翰逊
[1] McCandless
[2] 电影
[3] 明天明天
}

As you can see (based on the array shown above), the result should be:
$v1=hello
$v2=two words
$v3=string three

But how knows PHP how I want my strings to be separated?

My idea is doing the next:

1) Get the {2}{3} block as a single one.
2) Check in the array if this block (two words and three words) is in_array()
3) If NOT:
4) Remove the last word of it
5) Check again with the new (two words and three).
6) If NOT:
<<<<
4') Remove the last word of it
5') Check again with the new (two words and).
4'') Remove the last word of it
5'') Check again with the new (two words).
<<<<
7) Repeat 4 and 5 till it is one possible match (in_array())
8) The matched one will be {2} and the rest of the string will be {3}

My question: how can I make this, in PHP?
I tried to explain it the most simplified I could, and I hope you understand what I am trying to ask for. If anyone needs more examples I will write them down, just let me know. Thanks for reading.

EDIT --------------------------------------------------------------------
A real example:

Array: possible_matches{
[0] Christopher Johnson
[1] McCandless
[2] cinema
[3] tomorrow at night
}

模型:我的名字是 {1} {2} ,然后我要转到 {3} {4}

MODEL: My name is {1} {2}, and I am going to the {3}{4}

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

CASE: My name is Christopher Johnson McCandless, and I am going to the cinema tomorrow at night

所需结果:
$v1 = 克里斯托弗·约翰逊
$v2 = McCandless
$v3 = 电影
$v4 = 晚上明天

创建可能的组合数组

Desired result:
$v1=Christopher Johnson
$v2=McCandless
$v3=cinema
$v4=tomorrow at night

Create possible combinations array

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

注释中引用的

更新 ACCEPTED 答案

UPDATE referenced in comments for the ACCEPTED answer

$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)

推荐答案

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

可能形成两个组的组合是:

possible combination for forming two groups is:

  • Christopher Johnson McCandless
  • Christopher Johnson McCandless

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

可能形成两个组的组合是:

possible combination for forming two groups is:

  • cinema tomorrow at night
  • cinema tomorrow at night
  • cinema tomorrow at night

将PHP函数编写为 get_possible_groups($string_of_words, $group_count)返回组组合数组的数组.

Write a PHP function to get_possible_groups($string_of_words, $group_count) returns array of array of group combinations.

和一条SQL语句,如:

and an SQL statement like:

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');

一个可能的输出可能是:

one possible output can be:

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

答案是2(两个词组).

whichever has count 2(two word groups) that's your answer.

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

If MODEL text is a fulltext indexed column then for any given random string you can get most relevant model like:

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

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

query might return you something like:

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

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

Extracting the words for random string using placeholders from 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());
?>

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

Above code will get you values like:

'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查询以检查允许的可能匹配项
  • 所需分组中的实际单词.
    • from there you can call get possible groupings
    • then SQL query to check against allowed possible matches
    • actual words in required groupings.
    • A,这是一个问题,嗯!

      Alas, it's some question, eh!

      这篇关于PHP字符串差异和动态限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆