0

I am trying to insensitive replace string, so I'm using str_ireplace() but it does something I dont want and I have no clue how to overcome this problem.

so this is my code:

$q = "pArty";
$str = "PARty all day long";
echo str_ireplace($q,'<b>' . $q . '</b>',$str);

The output will be like that: "<b>pArty</b> all day long". The output looks like that because I replace insenitive variable $q with its sensitive.

How can I overcome this so the output will be "<b>PARty</b> all day long"?

Ron
  • 3,975
  • 17
  • 80
  • 130
  • did you read you're question ? `$str = "PARty all day long";` so why would you need str_ireplace ?? i suppose you forgot to mention smth or please edit you're question couse it doens't make any sence to use str_ireplace giving the output you whant ! – Poelinca Dorin Mar 07 '11 at 18:48
  • I only edited your question for formatting, but for future reference, you can see the edit history by clicking the `edited [some time] ago` link at the bottom of the question. It works like a wiki. – Matt Ball Mar 07 '11 at 18:56
  • @poelince, yes I had typo. I meant to write `$str = "PARty all day long"` – Ron Mar 07 '11 at 18:58

2 Answers2

3

You could do this with preg_replace, e.g.:

$q = preg_quote($q, '/'); // in case it has characters of significance to PCRE
echo preg_replace('/' . $q . '/i', '<b>$0</b>', $str);
Long Ears
  • 4,886
  • 1
  • 21
  • 16
0

Its because you are asking to replace " P A R t y" with "p A r t y". As commanded, php obeyed. However, why do you even want to replace anything? The original is the word you require.

frostymarvelous
  • 2,786
  • 32
  • 43
  • I got autocomplete plugin so I wanted to bold the querystring in the results. I am using "like" condition so it return insensitive results, therefore I need to insensitive replace. – Ron Mar 07 '11 at 18:55