0

これが私の問題です:私は大きな文字列(8000文字近く)を持っていて、2つのものが欲しいです:

  1. '。'のような文の境界を検出します と
  2. 600文字以下の文を使用する

場合によっては、両方を使用できないこともあります。この場合、スペースを見つけて文を分割します。

条件数1のridgerunnerによるこのソリューションは、魅力のように機能しました。元のリンク( http://goo.gl/PqI6d)を参照してください。ただし、600文字を超える文が出力されることがよくあります。何か光?前もって感謝します!

4

2 に答える 2

0

代わりに文字列を一致させたほうがよいでしょう。一致の正規表現は次のようになります。

(.{0,600}?\.)|(.{0,600}(?=\ ))

つまり、ピリオドの前にできるだけ小さい文字列を最初に探します。何もない場合は、できるだけ長い文字列とそれに続くスペースを探します。その後、次の試合は中断したところから始まります。

これは一般的な正規表現であることに注意してください。PHP の実装は異なる場合があります。

于 2012-07-09T05:43:14.737 に答える
0

Tks nhahtdh。何か足りないものがないか見てください。以下は、私の文字列からの抜粋と、あなたの提案を使用した出力です。

<?php 
    $ptn = "/(?:[^.]{1,600}(?: |\.)|\w{600,}(?: |\.)?)/";
    $str = "Amblyopia occurs when the nerve pathway from one eye to the brain does not develop during childhood. This occurs because the abnormal eye sends a blurred image or the wrong image to the brain. This confuses the brain, and the brain may learn to ignore the image from the weaker eye. Strabismus is the most common cause of amblyopia. There is often a family history of this condition. The term "lazy eye" refers to amblyopia, which often occurs along with strabismus. However, amblyopia can occur without strabismus and people can have strabismus without amblyopia.First, any eye condition that is causing poor vision in the amblyopic eye (such as cataracts) needs to be corrected. Children with a refractive error (nearsightedness, farsightedness, or astigmatism) will need glasses. Next, a patch is placed on the normal eye. This forces the brain to recognize the image from the eye with amblyopia. Sometimes, drops are used to blur the vision of the normal eye instead of putting a patch on it. Children whose vision will not fully recover, and those with only good eye due to any disorder should wear glasses with protective polycarbonate lenses. Polycarbonate glasses are shatter- and scratch-resistant. Children who get treated before age 5 will usually recover almost completely normal vision, although they may continue to have problems with depth perception. Delaying treatment can result in permanent vision problems. After age 10, only a partial recovery of vision can be expected. Early recognition and treatment of the problem in children can help to prevent permanent visual loss. All children should have a complete eye examination at least once between ages 3 and 5. Special techniques are needed to measure visual acuity in a child who is too young to speak. Most eye care professionals can perform these techniques.";
    preg_split($ptn, $str, -1, PREG_SPLIT_NO_EMPTY);
    print_r($result);
    ?>

結果: 600 文字未満の文字列からの文が必要です

 Array
(
[0] => childhood.
[1] => brain.
[2] => eye.
[3] => amblyopia.
[4] => condition.
[5] => strabismus.
[6] => amblyopia.
[7] => corrected.
[8] => glasses.
[9] => eye.
[10] => amblyopia.
[11] => it.
[12] => lenses.
[13] => scratch-resistant.
[14] => perception.
[15] => problems.
[16] => expected.
[17] => loss.
[18] => 5.
[19] => speak.
[20] => techniques
)
于 2012-07-09T15:14:19.293 に答える