str_replace PHP脚本无法强大地处理外部字符,如变音符号

时间:2013-10-14 00:16:08

标签: php apache str-replace diacritics

以下脚本并不总是正确捕获和转换外来字符。有人能告诉我我缺少什么才能让它变得更强大吗?

<?php
include("../index_head.inc.php");
$content = implode("",(@file("current.txt")));

$url = "http://XXXXXX.html?no_body=1";
$content = file_get_contents($url,'r');

if (isset($_GET['showcurrent']) && $_GET['showcurrent'] == '')
    {
    $content = substr($content,1,strpos($content,"<hr ")-1);
    }
    else 
    {
    $content = str_replace("<br style=\"clear:both\" />\n</p>", "</p>",$content);   
    $content = str_replace("ck1\"><img", "ck1\" target=_blank><img",$content);  
    };

$content = str_replace("<h3>current</h3>", "",$content);

echo "<div id=\"service\" style=\"width: 660px;padding-left:5px\">",str_replace("current.html","current.html",$content),"</div>";

include("../index_footer.inc.php");
?>

新信息:Pekka,你给我的想法是检查页面如何在没有str_replace()的情况下发出:

<?php
include("../index_head.inc.php");
$content = implode("",(@file("current.txt")));
$url = "XXXXXX.html?no_body=1";
$content = file_get_contents($url,'r');
echo "<div id=\"service\" style=\"width: 660px;padding-left:5px\">",$content,"</div>";

似乎问题出在其他地方,因为即使没有使用str_replace(),我也会得到相同的错误!如果你能帮助我解决这个问题,我一定会很感激。我看过你的愿望清单。 ;)

2 个答案:

答案 0 :(得分:3)

你在php中包含了charset吗?

试试这个:

header('Content-Type: text/html; charset=utf-8');

如果不工作,请检查您的文件是否已在str替换之前保存在utf8中:

utf8_encode ( string $data );

在相反的情况下使用:

utf8_decode( string $data );

希望它有所帮助!

答案 1 :(得分:0)

谢谢SBO - 确实有帮助!我只是将代码更改为:

<?php
include("../index_head.inc.php");
$content = implode("",(@file("current.txt")));

$url = "http://XXXXXX.html?no_body=1";
$content = file_get_contents(utf8_encode($url),'r');

if (isset($_GET['showcurrent']) && $_GET['showcurrent'] == '')
    {
    $content = substr($content,1,strpos($content,"<hr ")-1);
    }
else 
{
$content = str_replace("<br style=\"clear:both\" />\n</p>", "</p>",$content);   
$content = str_replace("ck1\"><img", "ck1\" target=_blank><img",$content);  
};

$content = str_replace("<h3>current</h3>", "",$content);

echo "<div id=\"service\" style=\"width: 660px;padding-left:5px\">",str_replace("current.html","current.html",utf8_decode($content)),"</div>";

include("../index_footer.inc.php");
?>

一切正常。 非常感谢你的帮助。