doulao2916 2012-06-07 11:54
浏览 67
已采纳

使用带有html特殊字符代码的preg_match替换无效的html特殊字符

<?php
    $tr_no = mysql_num_rows($query);
    $ii = 0; //iterator;
    while($news = mysql_fetch_array($query))

        //replacing special characters
        $replacename = eregi_replace(" ",  "-", $news['3']);
        $pattern = array();
        $pattern[0] = '-';
        $pattern[1] = "'";
        $replace = array();
        $replace[0] = '&#45;';
        $replace[1] = '&#39;';
        $news['2'] = preg_replace($pattern,$replace,$news['2']);
        $news['1'] = preg_replace($pattern,$replace,$news['1']);
        echo "<div style='width: 600px;' align='center'>";
        if($ii % 2 == 0){
            echo "<div style='width: 290px; height: 350px; float: left; padding:5px;'><h3 style='color:#ff0000; font-size: 14px;'>".$news['2']."</h3><p style='font-size:12px;'>".$news['1']."...</p><p><a href=\"".$href ."/". $replacename ."/item/".$news['0']."\">read more...</a></p></div>";
        }
        if($ii % 2 == 1){
            echo "<div style='width: 290px; height: 350px; float: left; padding:5px;'><h3 style='color:#ff0000; font-size: 14px;'>".$news['2']."</h3><p style='font-size:12px;'>".$news['1']."...</p><p><a href=\"".$href ."/". $replacename ."/item/".$news['0']."\">read more...</a></p></div>";
        }
        echo "</div>";
        $ii++;
    }
?>

The code output strings from the database while looping, but some characters appear invalid in an html format, see details: http://ipaidabribenaija.com/newsletter2.php I have also used str_replace() in place of preg_replace(), but the functions are not effective in this situation.

Please how can i achieve this task?

  • 写回答

2条回答 默认 最新

  • duanchijie2323 2012-06-07 12:02
    关注

    try

    $str = mb_decode_numericentity($str, array(0, 0x10FFFF, 0, 0xFFFFFF), 'UTF-8');
    

    ie

    $news[2] =  mb_decode_numericentity($news[2], array(0, 0x10FFFF, 0, 0xFFFFFF), 'UTF-8');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题