我的问题是,我不知道从哪里在我的输出字符,谁能解释我为什么在我的字符串是这些字符和我做什么来取消设置它们?
函数是用来改变"a","o","u"到"ae","oe","问题"
<?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>';
?>
输出:tae�p@tecmax.com
星期二�p@tecmax.com
脚趾�p@tecmax.com
你可以这样做:
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;
输出taep@tecmax.com
tuep@tecmax.com
toep@tecmax.com
使用功能
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;
}
您需要更改您的文件编码,或者使用ä
, ü
, ö
代替ä
, ü
, ö
。