2012-05-28 145 views
1

此函數在文本內搜索單詞(從$ words數組中)並突出顯示它們。在文本中搜索單詞並突出顯示包含它的所有單詞的函數

function highlightWords(Array $words, $text){ // Loop through array of words 
    foreach($words as $word){ // Highlight word inside original text 
     $text = str_replace($word, '<span class="highlighted">' . $word . '</span>', $text); 
    }   
    return $text; // Return modified text 
} 

這裏的問題是:

比方說的話$ =陣列( 「汽車」, 「驅動器」);

是否有功能突出,不僅字車的方式,而且還包含了字母詞「汽車」,如:汽車,卡爾馬尼亞等

謝謝!

回答

1

你想要的是(回撥你的情況將被推薦)正則表達式,preg_replace函數或peg_replace_callback更特別

<?php 
$searchString = "The car is driving in the carpark, he's not holding to the right lane.\n"; 

// define your word list 
$toHighlight = array("car","lane"); 

因爲你需要一個正則表達式來搜索你的話,你可能會想要或需要隨着時間的推移變化或變化,將它硬編碼到搜索詞中是不好的做法。因此,最好走路array_map陣列上,改造搜索內容到適當的正則表達式(這裏只是圍住進行/並增加了「接受一切,直到標點」表達)

$searchFor = array_map('addRegEx',$toHighlight); 

// add the regEx to each word, this way you can adapt it without having to correct it everywhere 
function addRegEx($word){ 
    return "/" . $word . '[^ ,\,,.,?,\.]*/'; 
} 

下一步要更換用高亮顯示的版本找到的詞,這意味着您需要動態更改:使用preg_replace_callback而不是常規preg_replace,以便它爲每個找到的匹配調用一個函數,並使用它來生成正確的結果。在這裏,我們在它的跨度標籤

function highlight($word){ 
    return "<span class='highlight'>$word[0]</span>"; 
} 

$result = preg_replace_callback($searchFor,'highlight',$searchString); 

print $result; 

封閉發現字產生

The <span class='highlight'>car</span> is driving in the <span class='highlight'>carpark</span>, he's not holding to the right <span class='highlight'>lane</span>. 

所以才貼上後,其他這些代碼片段,以獲得工作代碼,效果顯着。 ;)

編輯:下面的完整代碼被修改了一些=放在例程中,以方便原始請求者使用。 +不區分大小寫

完整代碼:

<?php 

$searchString = "The car is driving in the carpark, he's not holding to the right lane.\n"; 
$toHighlight = array("car","lane"); 

$result = customHighlights($searchString,$toHighlight); 

print $result; 

// add the regEx to each word, this way you can adapt it without having to correct it everywhere 

function addRegEx($word){ 
    return "/" . $word . '[^ ,\,,.,?,\.]*/i'; 
} 

function highlight($word){ 
    return "<span class='highlight'>$word[0]</span>"; 
} 

function customHighlights($searchString,$toHighlight){ 

// define your word list 
$searchFor = array_map('addRegEx',$toHighlight); 
$result = preg_replace_callback($searchFor,'highlight',$searchString); 
return $result; 

} 
+0

Ty非常感興趣...我有一個funtion.php文件。我會複製那裏的功能,但不知道在哪裏粘貼其他部分。 – webmasters

+0

我在下面添加了一個完整的代碼,供您複製/粘貼:) –

+0

非常感謝,請允許我試試:)我是一名seo,web開發人員,有時候可以通過程序員幫助,真的,真的幫助:) – webmasters

0

我沒有測試過,但我認爲這應該這樣做: -

$text = preg_replace('/\W((^\W)?$word(^\W)?)\W/', '<span class="highlighted">' . $1 . '</span>', $text); 

這看起來對於一個完整的字有界裏面的字符串,然後使用的preg_replace和定期把跨度周圍一大堆表達式。

+0

讓我測試了一下,我這樣一個福利局,將在一萬年也未曾想到這:) – webmasters

+0

當心,我只是改變了「W」到「W」來抓住單詞界限! – Purpletoucan

+0

嗯,dreamweaver說php語法有點問題 – webmasters

0
function replace($format, $string, array $words) 
{ 
    foreach ($words as $word) { 
     $string = \preg_replace(
      sprintf('#\b(?<string>[^\s]*%s[^\s]*)\b#i', \preg_quote($word, '#')), 
      \sprintf($format, '$1'), $string); 
    } 
    return $string; 
} 


// courtesy of http://slipsum.com/#.T8PmfdVuBcE 
$string = "Now that we know who you are, I know who I am. I'm not a mistake! It 
all makes sense! In a comic, you know how you can tell who the arch-villain's 
going to be? He's the exact opposite of the hero. And most times they're friends, 
like you and me! I should've known way back when... You know why, David? Because 
of the kids. They called me Mr Glass."; 

echo \replace('<span class="red">%s</span>', $string, [ 
    'mistake', 
    'villain', 
    'when', 
    'Mr Glass', 
]); 

正弦它利用周圍的字符串格式sprintf,可以相應地改變你的更換。

藉口5.4語法

+0

非常感謝。這將工作的帽子,像拉動反派和惡棍? – webmasters

+0

@webmasters Yeop,不區分大小寫(*表達式末尾的'i'修飾符'... \ b#i' *) – Dan

+0

請記住; '$ format'參數需要''%s'「在某個地方工作,因爲'sprintf'替換了這個標記;同樣,'$ format'不應該包含「'$ 1'」,否則你可能會得到時髦的結果。 – Dan

相關問題