2

例A[簡略化されたもの]:------------------------------------------ --------------------------

モデル:文字列{1}{2}のkeeeeeepとoooon .. ..

CASE_A2つの単語とoooonに文字列helloとkeeeeeep ...

CASE_B何か他のものをひもでつなぎ、楽しみのためだけにキープしてください...

、、 ...というn個の変数とそれぞれの一致する値のリストを取得する必要があります。編集:変数名はプレースホルダーに応じて指定されることに注意してください。プレースホルダーは常にINTです。(数字は単語数ではなく単なるインデックスです)ケースAの場合 := hello = 2ワード=など...ケースB の場合:=その他=楽しみのためだけ=など... これらの値を取得するための参照を見ることができます両方の文字列の「定数」部分です。$v1$v2$vn





$v1
$v2
$vn


$v1
$v2
$vn

例B[ほぼ本物の]:---------------------------------------- ----------------------------

ここで、次のように、可能な一致がそれぞれ配列に保存されていると仮定します(実際の場合は長いデータベースです)。possible_matches { [0] string three <<<< [1] Oneword <<<< [2]その他 [3 ]ハリーポップポッター [4] 2つの言葉<<<< [5]魔法の言葉魔法の気持ち }








前の例では、各{n}プレースホルダー」が「定数」文字列で区切られているため、これは必要ありませんでした。しかし、これらの「プレースホルダー」が一緒になっている場合があります...したがって、可能な一致に一致する新しい方法を発明する必要があります(固定リスト)。

文字列{1}{2} {3}とoooon..のkeeeeeep

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

ご覧のとおり(上記の配列に基づく)、結果は次のようになります。
$v1= hello
$v2 = 2ワード
$v3=文字列3

しかし、PHPが文字列をどのように分離するかをどのように知っているのでしょうか?

私の考えは次のことです:

1){2}{3}ブロックを単一のブロックとして取得します。2)このブロック(2ワードと3ワード)
正しいかどうか配列をチェックインします 。3)そうでない場合: 4)最後のワードを削除します 。5)新しいブロック(2ワードと3ワード)でもう一度チェックします。 6)そうでない場合: <<<< 4')最後の単語を削除します 5')新しい単語でもう一度確認しますin_array()






(2つの単語と)
4'')最後の単語を削除します
5'')新しい(2単語)でもう一度確認します。
<<<< 7)一致する可能性
のある ものが1つになるまで4と5を繰り返します() 8)一致したものは{2}になり、残りの文字列は{3}になります私の質問:PHPでこれを作成するにはどうすればよいですか? 私はそれを私ができる限り簡単に説明しようとしました、そしてあなたが私が求めようとしていることを理解してくれることを願っています。誰かがもっと例を必要とするならば、私はそれらを書き留めます、ただ私に知らせてください。読んでくれてありがとう。編集 - - - - - - - - - - - - - - - - - - - - - - - - - -------------------実際の例: 配列:possible_matchesin_array()








{
[0]クリストファージョンソン
[1]マッキャンドレス
[2]シネマ
[3]明日の夜
}

モデル:私の名前は{1} {2}で、 {3}{4}に行きます

ケース:私の名前はクリストファー・ジョンソン・ マッキャンドレスです。明日夜に映画館に行きます。

望ましい結果:
$v1= Christopher Johnson
$v2 = McCandless
$v3 = Cinema
$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);
}

承認された回答コメントで参照されている更新

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

1 に答える 1

1

Christopher Johnson McCandlessマップされるとき{1}{2}

2つのグループを形成するための可能な組み合わせは次のとおりです。

  • Christopher JohnsonMcCandless
  • ChristopherJohnson McCandless

cinema tomorrow at nightマップされるとき{3}{4}

2つのグループを形成するための可能な組み合わせは次のとおりです。

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

get_possible_groups($string_of_words, $group_count)グループの組み合わせの配列の配列を返すPHP関数を 記述します。

および次のような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');

考えられる出力の1つは次のとおりです。

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

カウント2(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クエリを使用して、許可されている可能性のある一致をチェックします
  • 必要なグループの実際の単語。

ああ、それはいくつかの質問です、ええ!

于 2013-02-28T18:35:10.727 に答える