栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

突出显示段落中的关键字

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

突出显示段落中的关键字

如果它包含html(请注意,这是一个非常强大的解决方案):

$string = '<p>foo<b>bar</b></p>';$keyword = 'foo';$dom = new Domdocument();$dom->loadHtml($string);$xpath = new DomXpath($dom);$elements = $xpath->query('//*[contains(.,"'.$keyword.'")]');foreach ($elements as $element) {    foreach ($element->childNodes as $child) {        if (!$child instanceof DomText) continue;        $fragment = $dom->createdocumentFragment();        $text = $child->textContent;        $stubs = array();        while (($pos = stripos($text, $keyword)) !== false) { $fragment->appendChild(new DomText(substr($text, 0, $pos))); $word = substr($text, $pos, strlen($keyword)); $highlight = $dom->createElement('span'); $highlight->appendChild(new DomText($word)); $highlight->setAttribute('class', 'highlight'); $fragment->appendChild($highlight); $text = substr($text, $pos + strlen($keyword));        }        if (!empty($text)) $fragment->appendChild(new DomText($text));        $element->replaceChild($fragment, $child);    }}$string = $dom->saveXml($dom->getElementsByTagName('body')->item(0)->firstChild);

结果是:

<p><span >foo</span><b>bar</b></p>

与:

$string = '<body><p>foobarbaz<b>bar</b></p></body>';$keyword = 'bar';

您得到(为了便于阅读,分为多行):

<p>foo    <span >bar</span>    baz    <b>        <span >bar</span>    </b></p>

提防非dom解决方案(如

regex
str_replace
),因为突出显示诸如“
div”之类的东西有完全破坏HTML的趋势……这只会在正文中“突出显示”字符串,而不会在标签内……


编辑 由于需要Google样式的结果,因此这是一种处理方法:

function getKeywordStubs($string, array $keywords, $maxStubSize = 10) {    $dom = new Domdocument();    $dom->loadHtml($string);    $xpath = new DomXpath($dom);    $results = array();    $maxStubHalf = ceil($maxStubSize / 2);    foreach ($keywords as $keyword) {        $elements = $xpath->query('//*[contains(.,"'.$keyword.'")]');        $replace = '<span >'.$keyword.'</span>';        foreach ($elements as $element) { $stub = $element->textContent; $regex = '#^.*?((w*W*){'.      $maxStubHalf.'})('.      preg_quote($keyword, '#').      ')((w*W*){'.      $maxStubHalf.'}).*?$#ims'; preg_match($regex, $stub, $match); var_dump($regex, $match); $stub = preg_replace($regex, '\1\3\4', $stub); $stub = str_ireplace($keyword, $replace, $stub); $results[] = $stub;        }    }    $results = array_unique($results);    return $results;}

好的,所以要做的就是返回一个包含

$maxStubSize
单词的匹配数组(即该数字之前的一半,之后的一半)…

因此,给定一个字符串:

<p>a whole     <b>bunch of</b> text     <a>here for</a>     us to foo bar baz replace out from this string    <b>bar</b></p>

调用

getKeywordStubs($string, array('bar', 'bunch'))
将导致:

array(4) {  [0]=>  string(75) "here for us to foo <span >bar</span> baz replace out from "  [3]=>  string(34) "<span >bar</span>"  [4]=>  string(62) "a whole <span >bunch</span> of text here for "  [7]=>  string(39) "<span >bunch</span> of"}

所以,那么您可以通过对列表进行排序

strlen
,然后选择两个最长的匹配项来构建结果blurb (假设php 5.3+):

usort($results, function($str1, $str2) {     return strlen($str2) - strlen($str1);});$description = implode('...', array_slice($results, 0, 2));

结果是:

here for us to foo <span >bar</span> baz replace out...a whole <span >bunch</span> of text here for

我希望有帮助…(我确实觉得这有点......肿…我敢肯定有更好的方法可以做到这一点,但这是一种方法)…



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/387184.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号