问题
My problem is that i dont know from where those chars in my output, can anyone explain me why in my string are those characters and what ive to do to unset them ?
the function is used to change 'ä', 'ö', 'ü' into 'ae', 'oe', 'ue'
<?php
// str | string argument
// needle | searched char
// val | value
// pos | default 0 at start at offset zero
// pos | momently just working with default offset
function changeLetter($str, $needle, $val, $pos = 0) {
$mstr = "";
while (isset($str[$pos])) {
if (ord($str[$pos]) == ord($needle)) {
$mstr .= $val;
$pos++;
} else {
$mstr .= $str[$pos];
$pos++;
}
}
return $mstr;
}
echo changeLetter("täp@tecmax.com", 'ä', 'ae') . '<br>';
echo changeLetter("tüp@tecmax.com", 'ü', 'ue') . '<br>';
echo changeLetter("töp@tecmax.com", 'ö', 'oe') . '<br>';
//echo changeLetter("täp@tecmax.com", 'ä', 'ae', 3) . '<br>';
?>
Output:
tae�p@tecmax.com
tue�p@tecmax.com
toe�p@tecmax.com
回答1:
Here is what you can do :
echo changeLetter("täp@tecmax.com", 'ä', 'ae'), PHP_EOL;
echo changeLetter("tüp@tecmax.com", 'ü', 'ue'), PHP_EOL;
echo changeLetter("töp@tecmax.com", 'ö', 'oe'), PHP_EOL;
Output
taep@tecmax.com
tuep@tecmax.com
toep@tecmax.com
Function Used
function changeLetter($str, $needle, $val, $pos = 0) {
$next = function ($str, &$pos) {
if (! isset($str[$pos]))
return false;
$char = ord($str[$pos]);
if ($char < 128) {
return $str[$pos ++];
} else {
if ($char < 224) {
$bytes = 2;
} elseif ($char < 240) {
$bytes = 3;
} elseif ($char < 248) {
$bytes = 4;
} elseif ($char = 252) {
$bytes = 5;
} else {
$bytes = 6;
}
$str = substr($str, $pos, $bytes);
$pos += $bytes;
return $str;
}
};
$mstr = "";
while(($chr = $next($str, $pos)) !== false) {
$mstr .= $chr == $needle ? $val : $chr;
}
return $mstr;
}
回答2:
you need to change your file encoding, or use ä, ü, ö instead of ä, ü, ö.
来源:https://stackoverflow.com/questions/16718705/php-undefined-chars-in-output