php - str_replace() 没有按预期工作

标签 php string encoding str-replace html-entities

我有这样一个字符串:

$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) 就可以看到这一点。然后你会看到:&lt;而不是 < .

解码回来只需使用 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/

相关文章:

php - 数据库驱动的选择框在提交时为空

c# - C# 6 中的字符串插值和重写的 ToString()

c++ - 使用 std::cin.getline() 输入一个 char 数组,无法读取整数

php - 如何用PHP解析特殊字符到MySQL?

php - 我的 Web 服务不工作 PHP/MYSQL/JSON

php - 如何在 PHP 中写入控制台?

javascript - JQuery无响应点击功能

php - PHP 中如何将数组赋值给字符串?

Java File.listFiles() 根据 'not exist'返回做 `exists()`的文件

ruby - 指定字符编码的方式有什么区别?