Domanda

The following script does not always correctly catch and convert foreign characters. Could someone show me what I'm missing to get it to be more robust?

<?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");
?>

New information: Pekka, you gave me the idea to check how the page emits without 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>";

It seems the problem lies elsewhere because I get the same mangling even without using str_replace()! If you can help me get this sorted out, I would sure appreciate it. I have seen your wish list. ;)

È stato utile?

Soluzione

Did you include the charset in php?

try this:

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

If not working check if your file is already saved in utf8 before str replace:

utf8_encode ( string $data );

In the opposite case use:

utf8_decode( string $data );

Hope it helps!

Altri suggerimenti

Thank you SBO - It sure did help! I simply changed the code to:

<?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");
?>

and everything is working fine. Thank you very much for your help.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top