php - 使用 str_replace 转换符号

标签 php xhtml

我们有一个新闻站点,我正在尝试设置一个 str_replace 来将 & 从数据库中提取的位置转换为标准友好的 & (用户仍然看到 &)

发生的是我在可见站点上输出了以下示例:

Changing World".  The report
Copper & Gold

而不是预期的空格或符号。数据是从带有文本字段的 mysqldb 中提取的。我哪里错了?

我的代码是:

function textfix($text){
    $find = array('&', '<br>');
    $replace = array('&amp;', '<br />');
    $newtext = str_replace($find, $replace, $text);
    return $newtext;
}

html 的内容是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

最佳答案

这与   无关,但是如果你的 $text 中有   它将被更改为 &nbsp;

还请告诉我们您在该 html 片段的源代码中看到了什么

关于php - 使用 str_replace 转换符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6692718/

相关文章:

php - 有没有更快的方法使用 php 将数据从文件导入 MySQL?

css - 寻找构建良好的 XHTML/CSS/JSP2 规范/最佳实践的引用

javascript - jQuery 选择器 + SVG 不兼容?

javascript - 打印带有标记的谷歌地图

php - Zend_PDF : Remove html contents from PDF document

php - 是否可以限制在 PHP 中调用方法的方式?

php - laravel 中区分大小写的 where 语句

jquery - 折叠时动画跳转

css - 我们是否应该始终为要使用内容 <p> 的 body {font-size :. ..} 保留相同的值?

css 根据列是否存在且具有 1 组规则来使用动态 1、2、3 列布局