2011-12-20 16 views
1

私はタイトルが理解するのはかなり複雑であることを知っています。 基本的に私は約20000文字を言うことができるテキストを持っている。単語の配列を強調表示する文字列の文の最初の大文字から最後に一致する正規表現

私は検索を実行するとき、一致する単語が見つかった文を抽出してハイライトしたいと思います。

$ wordsという名前のハイライト表示の配列があり、メインテキスト$ textを呼び出すことができます。 は、だから私のコードは次のようである:2つの単語が同じ文に属しているときに、このコードで

foreach($words as $word): 

    $regex = '/[^.!?\n]*\b'.preg_quote($word,"/").'\b[^.!?\n]*/i'; 

    preg_match_all($regex, $text, $matches); 
    count($matches[0]) > 3 ? $search_q= 3 : $search_q=count($matches[0]); 

    for ($i=0; $i < $search_q; $i++): 
     echo preg_replace('/\b('.preg_quote($word,"/").')\b/i','<span class="highlighted">$1</span>',$matches[0][$i]).'[..] '; 
    endfor; 
endforeach; 

問題があり、その後の文は二回印刷されます。私は両方の単語を強調表示して一度だけ印刷したいと思いますが、それを行う方法の手掛かりはありません。ヘルプみんなのため

おかげ

UPDATE:テストシナリオ

はそのsuposeます:

$text="A new holiday shopping tradition: Smartphones and social networks 

Many consumers will take out their phones before their wallets this holiday season with even more visiting social media sites before tackling their gift lists. 

More than one-quarter (27 percent) of smartphone owners plan to use their devices for holiday shopping to search for store locations (67 percent), compare prices (59 percent) and check product availability (46 percent). Additionally, 44 percent say they plan to use social media to seek discounts, read reviews and check family and friends’ gift lists. 

“Consumers are using online and mobile platforms to make the most of their holiday budgets, and the survey indicates that they will do more than just compare prices,” said Paul. “Retailers that use mobile and online channels to show product availability, locations and pricing but add customized promotions and gift ideas may encourage shoppers to come in the door for a specific gift and take additional items to the register.”"; 

と言葉は、次のとおりです。私はこれを取得私のコードでは

$words=array('social','media'); 

A new holiday shopping tradition: Smartphones and **social** networks[..] 
Many consumers will take out their phones before their wallets this holiday season with even more visiting **social** media sites before tackling their gift lists[..] 
Additionally, 44 percent say they plan to use **social** media to seek discounts, read reviews and check family and friends’ gift lists[..] 
Many consumers will take out their phones before their wallets this holiday season with even more visiting social **media** sites before tackling their gift lists[..] 
Additionally, 44 percent say they plan to use social **media** to seek discounts, read reviews and check family and friends’ gift lists[..] 

は、代わりに私がしたい:私が得るFGEコードで

A new holiday shopping tradition: Smartphones and **social** networks[..] 
Many consumers will take out their phones before their wallets this holiday season with even more visiting **social** **media** sites before tackling their gift lists[..] 
Additionally, 44 percent say they plan to use **social** **media** to seek discounts, read reviews and check family and friends’ gift lists[..] 

social[..] 
social[..] 
social[..] 
media[..] 
media[..] 

私が理解する例とその簡単なことを願っています。ありがとうございます

答えて

0

最初に、私はあなたがそのような複雑な正規表現を使用する理由を知りません。単語アンカーを使用するので、なぜ補完された文字クラスを気にしませんか?

第二に、このソリューションは、言葉は特別な正規表現文字が含まれていない...ここで

はあなたが何ができるかであることを前提としています

$w = preg_quote($word, "/"); 
$fullword = '\b\Q' . $w . '\E\b'; 
$regex = '/' . $fullword . '(?!.*' . $fullword . ')/i'; 

説明:\Qは、すべての文字を意味\Eまで、すべては文字通り(単語にドットが含まれていればあなたが安全であることを意味します)とみなされます。だから、あなたはあなたの言葉にマッチします(アンカーされています)。そして、あなたは再び単語に一致させるべきではないと言います。(?!.*\b\Qwordhere\E\b)

これは、文章に単語が数回含まれている場合、最後の出現にのみ一致することを意味します。

最後に、強調するために、使用:

preg_replace('/(' . $fullword . ')/ig', '<span class="highlighted">$1</span>', $text); 
+0

私はあなたのコードを試しましたが、私はちょうど完全な文ではなく、言葉を得ています。私は – chifliiiii

1

あなたの頭は、おそらくあなたがテキストを分割した場合の文章の配列にあまりを傷つけるし、順番にそれぞれの文を検討します。単語のリストが長すぎない場合は、リスト全体をあなたの正規表現に入れることができます。何かが好き:

/\b(\Qword1\E|\Qword2\E|\Qword3\E)\b/ 
+0

の例で質問を更新します申し訳ありませんが私のコード、im regex null guy:Dのこれを実装する方法を知っていません。私は例を使って質問を更新しました。 – chifliiiii

関連する問題