我有这样一个字符串:
$string = "somehting<br><br>";
当我像这样尝试 str_replace()
时:
echo str_replace("<br>","<c>",$string);
输出字符串根本没有改变。
我尝试过的:
echo mb_detect_encoding($string); // returns UTF-8
var_dump(strpos($string, "<br>")); // bool(false)
我用 html dom node class 编辑了字符串以前和现在我真的不知道哪里出了问题。
我尝试用真实字符串在 sandbox.onlinephpfunctions.com 上重现该问题,它有效。
所以我无法重现这个问题。
最佳答案
事实证明。您的字符串是用 html 实体编码的。因此,您只需查看源代码或使用 highlight_string($yourString)
就可以看到这一点。然后你会看到:<
而不是 <
.
解码回来只需使用 html_entity_decode()
.例如:
$string = str_replace("<br>","<c>", html_entity_decode($string));
highlight_string($string);
关于php - str_replace() 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31720138/