Question

Win7-HUN, UTF8 PHP file.

function Test($Msg){

 $Result = "";
 $ReplFrom = "ő";
 $ReplTo = "ö";

 for($i=0; $i<mb_strlen($Msg); $i++){
 $c = mb_substr($Msg, $i, 1);
 echo($c);echo("<br>");
 }
}

Test('őű');

This showing good: "őű"

I want to replace "ő" with "ö" but one-by-one, because I want to check many other things.

function Test($Msg){

 $Result = "";
 $ReplFrom = "ő";
 $ReplTo = "ö";

 for($i=0; $i<mb_strlen($Msg); $i++){
 $c = mb_substr($Msg, $i, 1);
 $add = $c;
 $h = mb_substr($ReplFrom, 0, 1);
 if ($c == $h) {
 $add = $ReplTo;
 echo("found");
 }
 $Result = $Result.$add;
 }

 return $Result;

} 

Interesting that is found all chars, and the result is wrong.

Simplified to concat:

 $what = 'ő';
 $replto = "ö";
 echo($what.$replto); 

This is good.

 $what = mb_substr('ő', 0, 1);
 $replto = mb_substr("ö", 0, 1);
 echo($what.$replto); 

This is wrong.

What I do wrong? I want to step in every MB character, and check it. If some of them needed, I must replace. If some of them illegal, replace them with " ". Etc. And at the end concat to one string.

for example: "álmos ő körben + 2" "álmos ö körben 2"

But something is wrong in my code. I want to use UTF8 chars, because I have MultiByte input, and UTF8 or 16 XML. And some of the servers are english, some of hungarian (encoding is different).

But interesting that something is get wrong when I want to working with MB chars. Maybe the concatentation causes this?

Very-very thanks for every advance in this theme!

Regards: dd

Was it helpful?

Solution

You probably need to specify encoding for mb_ functions.

$c = mb_substr($Msg, $i, 1,'utf-8');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top