Approx
Approx

Reputation: 21

I need to replace all instances of [IMG] in a string using PHP

I've tried:

$input = str_replace('[IMG]',"",$input,$rplc);

with no success.

I've also tried escaping and double-escaping [IMG] by doing '[IMG]' and '\[IMG\]', neither worked. It doesn't throw any errors, but it doesn't actually replace anything either.

How can I get this to work?

Upvotes: 2

Views: 60

Answers (3)

Amit Verma
Amit Verma

Reputation: 41229

Try this

    preg_replace("@\[img\]@","",$input);

And the same result can also be achived by using

str_replace()

  str_replace("[img]",'',$input);

Upvotes: 0

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21422

Just try this example, to implement within your code..

 $search = "[IMG]";
 $replace = "";
 $string = "Hello [IMG] Approxx? How[IMG] are [IMG]You?";
 $input = str_replace($search, $replace, $string);
 echo $input;

Upvotes: 1

Mandi
Mandi

Reputation: 414

Have you tried something like:

$input = preg_replace("/(\\[IMG\\])/", $replacement, $input);

Upvotes: 1

Related Questions