2012-01-23 28 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上查詢標題結果時,您會從網頁獲取一些內容,並且查詢以粗體顯示。

+0

您的匹配有多模糊?精確匹配,還是與詞幹和這樣的規範化?你有搜索查詢的位置數據嗎? –

+0

請確定一個*詞*是什麼。 – hakre

回答

4

這裏是a的方法。我不是說這是最好的方式,但它會起作用。可能有一種正則表達方式可以做到「更好」或「更好」。

$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

這是你如何能做到的是一個相對簡單的例子:

<?php 

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

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

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

如果引號是可選的,並且希望關於特殊字符圓頂靈活使用

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

,如果你想匹配部分單詞使用

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

以匹配電話中的「hon」

+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單詞。

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

相關問題