2012-01-23 20 views
0

1000文字以上の大きな文字列があります。私が必要とするのは、特定の単語を見つけて、その単語の周りにある単語を変数にラップすることです。私はこれを達成するにはどうすればよい文字列内の特定の単語を見つけてラップします

$in = 'This is a very long sentence, what I need is to find the word "phone" in this sentence, and after that, to wrap some words around it'; 

:あなたが見ることができるように私は言葉「電話」を見つけたとき

$out = 'find the word "phone" in this sentence'; 

だから、、、私はその言葉の左&右に展開したいです。 実際の例は、Googleでクエリを実行すると、タイトルの結果を熟読し、ウェブページからコンテンツを取得し、クエリを太字にします。引用符はオプションであり、特殊文字に関するドームの柔軟性をしたい

preg_match('/\w+[^\w]+\w+[^\w]+\w+[^\w]+phone[^\w]+\w+[^\w]+\w+[^\w]+\w+/',$in,$m); 

とIFを使用している場合

<?php 

    $in = "blah blah blah test blah blah blah"; 
    $search = "test"; 
    $replace = "--- test ---"; 

    $out = str_replace($search, $replace, $in); 

?> 
+0

どのようにファジーが一致していますか?正確なマッチング、またはステミングとそのような正規化との一致検索クエリからのポジションデータはありますか? –

+0

*単語*が何であるかを定義してください。 – hakre

答えて

4

ここにはの方法があります。私はこれが最善の方法だと言っているわけではありませんが、うまくいくでしょう。おそらく、 "より良い"または "良い"という正規表現の方法があります。

$in = 'This is a very long sentence, what I need is to find the word phone in this sentence, and after that, to wrap some words around it'; 
$wordToFind = 'phone'; 
$numWordsToWrap = 3; 

$words = preg_split('/\s+/', $in); 
if (($pos = array_search($wordToFind, $words)) !== FALSE) { 
    $start = ($pos - $numWordsToWrap > 0) ? $pos - $numWordsToWrap : 0; 
    $length = (($pos + ($numWordsToWrap + 1) < count($words)) ? $pos + ($numWordsToWrap + 1) : count($words) - 1) - $start; 
    $slice = array_slice($words, $start, $length); 
    $out = implode(' ', $slice); 
    echo $out; 
} else echo 'I didn\'t find it'; 
+0

ありがとう、これはトリックでした。 –

2

これは、あなたがそれを達成できるかの比較的単純な例であります部分的に一致する単語を使用したい場合は

電話で "hon"と一致するように

2
$out=preg_match('/\w+\s+\w+\s+\w+\s+\"phone\"\s+\w+\s+\w+\s+\w+/',$in,$m); 
if ($out) $out=$m[0]; 

+0

'\ W == [^ \ w]' - パターンを読みやすくするかもしれません。また、['preg_quote'](http://php.net/preg_quote)の注釈が役に立つかもしれません。 – hakre

+0

@hakreあまりにもばかげて申し訳ありませんが、私はあなたのコメントを否定しません –

+0

'[^ \ w] +'を '\ W +'に置き換えることができます。 'preg_quote'を使って検索用語を挿入すると、パターンが壊れないようにすることができます。 – hakre

0
$in = 'This is a very long sentence, what I need is to find the word phone in this  sentence, and after that, to wrap some words around it'; 

$array = explode(" ", $in); 

$how_much = 3; 

$search_word = "phone"; 

foreach ($array as $index => $word) { 
    if ($word == $search_word) { 
     for ($index1 = 0; $index1 < ($how_much * 2) + 1; $index1++) { 
      $key = $index-$how_much+$index1; 
      echo $array[$key]; 
      echo " "; 
     } 
    } 
} 

これはシンプルなソリューションです。スペースで文章を爆発させてから、あなたの単語+ $ how_much wordsを両方向に表示してください。

5

正規表現ウェイ

あなたは、文字列に特定の単語(検索テキスト)を強調表示したい場合は、次の操作を行います。

PHPコード:

$in = 'This is a very long sentence, what I need is to find the word phone in this sentence, and after that, to wrap some words around it'; 
$wordToFind = 'phone'; 
$wrap_before = '<span class="highlight_match">'; 
$wrap_after = '</span>'; 

$out = preg_replace("/($wordToFind)/i", "$wrap_before$1$wrap_after", $in); 

// value of $out is now: 
// This is a very long sentence, what I need is to find the word <span class="highlight_match">phone</span> in this sentence, and after that, to wrap some words around it 

CSSコード

この例では、スパンクラスとマッチしたテキストをラップしているので、ここでは必須の例のCSSコード

<style type="text/css"> 
    .highlight_match { 
     background-color: yellow; 
     font-weight: bold; 
    } 
</style> 
4
があります@DaveRandomのおかげで

私はちょうどコードを改善し、書き直し

<?php 

$in = 'This is a very long sentence, what I need is to find the word phone in this sentence, and after that, to wrap some words around it'; 
$wordToFind = 'words'; 
$numWordsToWrap = 3; 
echo $in; 
echo "<br />"; 
$words = preg_split('/\s+/', $in); 

$found_words = preg_grep("/^".$wordToFind.".*/", $words); 
$found_pos  = array_keys($found_words); 
if(count($found_pos)) 
{ 
    $pos = $found_pos[0]; 
} 

if (isset($pos)) 
{ 
    $start = ($pos - $numWordsToWrap > 0) ? $pos - $numWordsToWrap : 0; 
    $length = (($pos + ($numWordsToWrap + 1) < count($words)) ? $pos + ($numWordsToWrap + 1) : count($words)) - $start; 
    $slice = array_slice($words, $start, $length); 

    $pre_start = ($start > 0) ? "...":"";  

    $post_end = ($pos + ($numWordsToWrap + 1) < count($words)) ? "...":""; 

    $out = $pre_start.implode(' ', $slice).$post_end; 
    echo $out; 
} 
else 
    echo 'I didn\'t find it'; 
?> 

あなたは皆再利用したいかもしれません。

さらにDaveRandomに感謝

関連する問題