Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

str_replace() doesn't work as expected

I have a string like this :

$string = "somehting<br><br>";

When I try str_replace() like this:

echo str_replace("<br>","<c>",$string);

The output string doesn't change at all.

What I tried:

echo mb_detect_encoding($string); // returns UTF-8
var_dump(strpos($string, "<br>")); // bool(false)

I have edited the string with html dom node class before and now I really don't know what is wrong.

I have tried to reproduce the problem on sandbox.onlinephpfunctions.com with real string and it works.

So I am unable to reproduce the problem.

like image 598
JTC Avatar asked Jul 19 '26 11:07

JTC


1 Answers

So as it turns out. Your string is encoded with html-entities. So you see this by simply looking into the source code or using highlight_string($yourString) and then you will see: &lt; instead of <.

To decode it back simply use html_entity_decode(). As example:

$string = str_replace("<br>","<c>", html_entity_decode($string));
highlight_string($string);
like image 62
Rizier123 Avatar answered Jul 22 '26 02:07

Rizier123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!