7

元のケーシングを破壊せずに str_ireplace を実行することは可能ですか?

例えば:

$txt = "Hello How Are You";
$a = "are";
$h = "hello";
$txt = str_ireplace($a, "<span style='background-color:#EEEE00'>".$a."</span>", $txt);
$txt = str_ireplace($h, "<span style='background-color:#EEEE00'>".$h."</span>", $txt);

これはすべて正常に機能しますが、結果は次のように出力されます。

[hello] How [are] You

それ以外の:

[Hello] How [Are] You

(角括弧は背景色です)

ありがとう。

4

3 に答える 3

4

これらの行に沿って何かが必要だと思います: 表示されている単語を見つけて、それを使用して置換を行います。

function highlight($word, $text) {
    $word_to_highlight = substr($text, stripos($text, $word), strlen($word));
    $text = str_ireplace($word, "<span style='background-color:#EEEE00'>".$word_to_highlight."</span>", $text);
    return $text;
}
于 2013-05-24T17:19:10.693 に答える