将字符串中的单词替换为高亮显示的单词,并保持其大小写不变


Replace words found in string with highlighted word keeping their case as found

我想用高亮显示的单词替换字符串中的单词,保持大小写不变。

示例

$string1 = 'There are five colors';
$string2 = 'There are Five colors';
//replace five with highlighted five
$word='five';
$string1 = str_ireplace($word, '<span style="background:#ccc;">'.$word.'</span>', $string1);    
$string2 = str_ireplace($word, '<span style="background:#ccc;">'.$word.'</span>', $string2);
echo $string1.'<br>';
echo $string2;

电流输出:

five颜色
five颜色

预期输出:

five颜色
Five颜色

如何做到这一点?

不区分大小写突出显示单个单词

preg_replace()与以下正则表达式一起使用:

/'b($p)'b/i

解释:

  • /-起始分隔符
  • 'b-匹配单词边界
  • (-第一个捕获组的启动
  • $p-转义的搜索字符串
  • )-第一个捕获组结束
  • 'b-匹配单词边界
  • /-结束分隔符
  • i-使搜索不区分大小写的模式修饰符

替换模式可以是<span style="background:#ccc;">$1</span>,其中$1是一个反向引用——它将包含第一个捕获组匹配的内容(在本例中,它是搜索的实际单词)

代码:

$p = preg_quote($word, '/');  // The pattern to match
$string = preg_replace(
    "/'b($p)'b/i",
    '<span style="background:#ccc;">$1</span>', 
    $string
);

在行动中看到它


不区分大小写地突出显示单词数组

$words = array('five', 'colors', /* ... */);
$p = implode('|', array_map('preg_quote', $words));
$string = preg_replace(
    "/'b($p)'b/i", 
    '<span style="background:#ccc;">$1</span>', 
    $string
);
var_dump($string);

在操作中查看

str_replace-区分大小写的

str_ireplace-类非约束

http://www.php.net/manual/en/function.str-replace.php

http://www.php.net/manual/en/function.str-ireplace.php

这是测试用例。

<?php
class ReplaceTest extends PHPUnit_Framework_TestCase
{
    public function testCaseSensitiveReplaceSimple()
    {
        $strings = array(
            'There are five colors',
            'There are Five colors',
        );
        $expected = array(
            'There are <span style="background:#ccc;">five</span> colors',
            'There are <span style="background:#ccc;">Five</span> colors',
        );
        $find = array(
            'five',
            'Five',
        );
        $replace = array(
            '<span style="background:#ccc;">five</span>',
            '<span style="background:#ccc;">Five</span>',
        );
        foreach ($strings as $key => $string) {
            $this->assertEquals($expected[$key], str_replace($find, $replace, $string));
        }
    }
    public function testCaseSensitiveReplaceFunction()
    {
        $strings = array(
            'There are five colors',
            'There are Five colors',
        );
        $expected = array(
            'There are <span style="background:#ccc;">five</span> colors',
            'There are <span style="background:#ccc;">Five</span> colors',
        );
        foreach ($strings as $key => $string) {
            $this->assertEquals($expected[$key], highlightString('five', $string, '<span style="background:#ccc;">$1</span>'));
        }
    }
}
/**
 * @argument $words array or string - words to that are going to be highlighted keeping case
 * @argument $string string - the search 
 * @argument $replacement string - the wrapper used for highlighting, $1 will be the word
 */
function highlightString($words, $string, $replacement)
{
    $replacements = array();
    $newwords = array();
    $key = 0;
    foreach ((array) $words AS $word)
    {
        $replacements[$key] = str_replace('$1', $word, $replacement);
        $newwords[$key] = $word;
        $key++;
        $newwords[$key] = ucfirst($word);
        $replacements[$key] = str_replace('$1', ucfirst($word), $replacement);
        $key++;
    }
    return str_replace($newwords, $replacements, $string);
}

结果

..
Time: 25 ms, Memory: 8.25Mb
OK (2 tests, 4 assertions)