您在更换字符串之前更改$content.
$content = "
This is a pre text I want to convert it to paragraphs ";
print_r(htmlspecialchars($content));
// returns <pre>This is a pre text I want to convert it to paragraphs
这些都与str_replace不匹配
删除htmlspecialchars(),您将获得所需的输出.
$content = "
This is a pre text I want to convert it to paragraphs ";
print_r(str_replace(array('
', '', ' '),array('
', '
', ''),$content));
//returns
This is a pre text
I want to convert it to paragraphs