7

is it possible to run str_ireplace without it destroying the original casing?

For instance:

$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);

this all works fine, but the result outputs:

[hello] How [are] You

instead of:

[Hello] How [Are] You

(square brackets being the color background)

Thanks.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
FoxyFish
  • 874
  • 1
  • 12
  • 21
  • 1
    Also i'd like to add, the reason i use ireplace is that i want it to change even in lowercase, but if uppercase i want it to keep its capital. – FoxyFish May 24 '13 at 17:04
  • internally `str_ireplace` converts `$search` and `$replace` to lowercase to find matches - it's not an answer, but explains why you're having the problem – naththedeveloper May 24 '13 at 17:06

3 Answers3

7

You're probably looking for this:

$txt = preg_replace("#\\b($a|$h)\\b#i", 
  "<span style='background-color:#EEEE00'>$1</span>", $txt);

... or, if you want to highlight the whole array of words (being able to use metacharacters as well):

$txt = 'Hi! How are you doing? Have some stars: * * *!';
$array_of_words = array('Hi!', 'stars', '*');

$pattern = '#(?<=^|\W)(' 
       . implode('|', array_map('preg_quote', $array_of_words))
       . ')(?=$|\W)#i';

echo preg_replace($pattern, 
      "<span style='background-color:#EEEE00'>$1</span>", $txt);
raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • @user1483508 Note that your original code from the question works as well. And that's why you should **not** use `preg_replace()` as it is slower. Don't understand what's the problem actually – hek2mgl May 24 '13 at 17:12
  • @hek2mgl: It's because i needed it to work for uppercase, lowercase and mixed without it changing it to lowercase in the output. – FoxyFish May 24 '13 at 17:16
  • The code you've posted will output the expected results. Please show exactly what is your current output and what is the expected output – hek2mgl May 24 '13 at 17:18
  • Can someone tell me what I'm missing here? – hek2mgl May 24 '13 at 17:19
  • You're not missing anything, problem was solved with the first post. I think i just worded my initial post wrong for some people. – FoxyFish May 24 '13 at 17:19
  • @hek2mgl I can, probably. ) The point of OP is being able to wrap both 'hello' and 'HELLO' in "Hello, this is some example of HELLO hello" string. `str_ireplace` will replace both of them with _one_ variant of 'hello' string - either uppercase of lowercase. – raina77ow May 24 '13 at 17:21
  • Thx for the explanation. Currently I still don't get it. Will read all again later, sometimes this helps ;) – hek2mgl May 24 '13 at 17:26
  • @hek2mgl Check this [demo](http://codepad.org/HGNgODWR), perhaps it'll help too. ) – raina77ow May 24 '13 at 17:28
  • 1
    Got it! :) thanks! +1 (already up-voted your post, can't do twice ;) – hek2mgl May 24 '13 at 18:29
  • You could just do what you're doing and then `ucwords($result)` the output – Bysander Jul 07 '15 at 12:14
4

I think you'd want something along these lines: Find the word as it is displayed, then use that to do the replace.

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;
}
mhanson01
  • 711
  • 6
  • 4
  • Thanks, this is what I was looking for too – Solvision Feb 27 '16 at 00:50
  • Actually this one seems to have an issue with multiple occurances of the search string, first one is fine (kept in original format) but subsequent ones get converted – Solvision Feb 27 '16 at 01:22
1

Not beautiful, but should work.

function str_replace_alt($search,$replace,$string)
{
    $uppercase_search = strtoupper($search);
    $titleCase_search = ucwords($search);
    $lowercase_replace = strtolower($replace);
    $uppercase_replace = strtoupper($replace);
    $titleCase_replace = ucwords($replace);

    $string = str_replace($uppercase_search,$uppercase_replace,$string);
    $string = str_replace($titleCase_search,$titleCase_replace,$string);
    $string = str_ireplace($search,$lowercase_replace,$string);

    return $string;
}
LeonanCarvalho
  • 1,819
  • 3
  • 20
  • 39
user1122069
  • 1,767
  • 1
  • 24
  • 52