2012-05-28 42 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

相关问题