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

时间:2012-06-07 11:54:27

标签: php mysql

<?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++;
    }
?>

代码在循环时从数据库输出字符串,但某些字符在html格式中显示为无效,请参阅详细信息:http://ipaidabribenaija.com/newsletter2.php 我还使用str_replace()代替preg_replace(),但在这种情况下函数无效。

请问我该如何完成这项任务?

2 个答案:

答案 0 :(得分:0)

尝试

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

$news[2] =  mb_decode_numericentity($news[2], array(0, 0x10FFFF, 0, 0xFFFFFF), 'UTF-8');

答案 1 :(得分:0)

如果不是编码/解码问题,为什么不尝试使用htmlentities php的功能?